home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / Gnuplot / Source / command.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-02  |  111.4 KB  |  4,255 lines

  1. #ifndef lint
  2. static char    *RCSid = "$Id: command.c%v 3.38.2.82 1993/03/01 00:28:27 woo Exp woo $";
  3. #endif
  4.  
  5.  
  6. /* GNUPLOT - command.c */
  7. /*
  8.  * Copyright (C) 1986 - 1993   Thomas Williams, Colin Kelley
  9.  * 
  10.  * Permission to use, copy, and distribute this software and its documentation
  11.  * for any purpose with or without fee is hereby granted, provided that the
  12.  * above copyright notice appear in all copies and that both that copyright
  13.  * notice and this permission notice appear in supporting documentation.
  14.  * 
  15.  * Permission to modify the software is granted, but not the right to distribute
  16.  * the modified code.  Modifications are to be distributed as patches to
  17.  * released version.
  18.  * 
  19.  * This software is provided "as is" without express or implied warranty.
  20.  * 
  21.  * 
  22.  * AUTHORS
  23.  * 
  24.  * Original Software: Thomas Williams,  Colin Kelley.
  25.  * 
  26.  * Gnuplot 2.0 additions: Russell Lang, Dave Kotz, John Campbell.
  27.  * 
  28.  * Gnuplot 3.0 additions: Gershon Elber and many others.
  29.  * 
  30.  * Changes:
  31.  * 
  32.  * Feb 5, 1992    Jack Veenstra    (veenstra@cs.rochester.edu) Added support to
  33.  * filter data values read from a file through a user-defined function before
  34.  * plotting. The keyword "thru" was added to the "plot" command. Example
  35.  * syntax: f(x) = x / 100 plot "test.data" thru f(x) This example divides all
  36.  * the y values by 100 before plotting. The filter function processes the
  37.  * data before any log-scaling occurs. This capability should be generalized
  38.  * to filter x values as well and a similar feature should be added to the
  39.  * "splot" command.
  40.  * 
  41.  * 19 September 1992  Lawrence Crowl  (crowl@cs.orst.edu)
  42.  * Added user-specified bases for log scaling.
  43.  * 
  44.  * Send your comments or suggestions to info-gnuplot@dartmouth.edu. This is
  45.  * a mailing list; to join it send a note to
  46.  * info-gnuplot-request@dartmouth.edu.  Send bug reports to
  47.  * bug-gnuplot@dartmouth.edu.
  48.  */
  49.  
  50. #include <stdio.h>
  51. #include <math.h>
  52. #include <ctype.h>
  53.  
  54. #ifdef AMIGA_AC_5
  55. #include <time.h>
  56. void            sleep();    /* defined later */
  57. #endif
  58.  
  59. #ifdef OS2
  60. #include <setjmp.h>
  61. extern jmp_buf env;       /* from plot.c */
  62. #endif
  63.  
  64. #if defined(MSDOS) || defined(DOS386)
  65. #ifdef DJGPP
  66. #include <dos.h>
  67. #else
  68. #include <process.h>
  69. #endif
  70.  
  71. #ifdef __ZTC__
  72. #define P_WAIT 0
  73. #include <time.h>        /* usleep() */
  74. #else
  75.  
  76. #ifdef __TURBOC__
  77. #ifndef _Windows
  78. #include <dos.h>        /* sleep() */
  79. #include <conio.h>
  80. #include <dir.h>    /* setdisk() */
  81. extern unsigned _stklen = 16394;/* increase stack size */
  82. #endif
  83.  
  84. #else                /* must be MSC */
  85. #if !defined(__EMX__) && !defined(DJGPP)
  86. #ifdef __MSC__
  87. #include <direct.h>        /* for _chdrive() */
  88. #endif
  89. #include <time.h>        /* kludge to provide sleep() */
  90. void            sleep();    /* defined later */
  91. #endif                /* !__EMX__ && !DJGPP */
  92. #endif                /* TURBOC */
  93. #endif                /* ZTC */
  94.  
  95. #endif                /* MSDOS */
  96.  
  97. #ifdef ATARI
  98. #ifdef __PUREC__
  99. #include <ext.h>
  100. #include <tos.h>
  101. #include <aes.h>
  102. #else
  103. #include <osbind.h>
  104. #include <aesbind.h>
  105. #endif /* __PUREC__ */
  106. #endif /* ATARI */
  107.  
  108. #ifdef AMIGA_SC_6_1
  109. #include <proto/dos.h>
  110. void            sleep();
  111. #endif                /* AMIGA_SC_6_1 */
  112.  
  113. #include "plot.h"
  114. #include "setshow.h"
  115. #ifndef _Windows
  116. #include "help.h"
  117. #else
  118. #define MAXSTR 255
  119. #endif
  120.  
  121. #ifndef STDOUT
  122. #define STDOUT 1
  123. #endif
  124.  
  125. #ifndef HELPFILE
  126. #if defined( MSDOS ) || defined( OS2 ) || defined(DOS386)
  127. #define HELPFILE "gnuplot.gih"
  128. #else
  129. #ifdef AMIGA_SC_6_1
  130. #define HELPFILE "S:gnuplot.gih"
  131. #else
  132. #define HELPFILE "docs/gnuplot.gih"    /* changed by makefile */
  133. #endif                /* AMIGA_SC_6_1 */
  134. #endif
  135. #endif                /* HELPFILE */
  136.  
  137. #ifdef _Windows
  138. #include <windows.h>
  139. #include <setjmp.h>
  140. #ifdef __MSC__
  141. #include <malloc.h>
  142. #else
  143. #include <alloc.h>
  144. #include <dir.h>    /* setdisk() */
  145. #endif
  146. #include "win/wgnuplib.h"
  147. void sleep();
  148. extern TW textwin;
  149. extern jmp_buf FAR env;       /* from plot.c */
  150. extern LPSTR winhelpname;
  151. extern void screen_dump(void);    /* in term/win.trm */
  152. extern int Pause(LPSTR mess); /* in winmain.c */
  153. #endif
  154.  
  155. #define inrange(z,min,max) ((min<max) ? ((z>=min)&&(z<=max)) : ((z>=max)&&(z<=min)) )
  156.  
  157. /*
  158.  * instead of <strings.h>
  159.  */
  160.  
  161. #ifndef ATARI
  162. #ifdef _Windows
  163. #include <string.h>
  164. #include <stdlib.h>
  165. #else
  166. #ifndef AMIGA_SC_6_1
  167. extern char    *gets(), *getenv();
  168. extern char    *strcpy(), *strncpy(), *strcat(), *strchr();
  169. extern int      strlen(), strcmp();
  170. extern double   atof();
  171. #endif /* !AMIGA_SC_6_1 */
  172. #endif
  173. #else
  174. #ifdef __PUREC__
  175. /*
  176.  * a substitute for PureC's buggy sscanf.
  177.  * this uses the normal sscanf and fixes the following bugs:
  178.  * - whitespace in format matches whitespace in string, but doesn't
  179.  *   require any. ( "%f , %f" scans "1,2" correctly )
  180.  * - the ignore value feature works (*). this created an address error
  181.  *   in PureC.
  182.  */
  183.  
  184. #include <stdarg.h>
  185. #include <string.h>
  186.  
  187. int purec_sscanf( const char *string, const char *format, ... )
  188. {
  189.   va_list args;
  190.   int cnt=0;
  191.   char onefmt[256];
  192.   char buffer[256];
  193.   const char *f=format;
  194.   const char *s=string;
  195.   char *f2;
  196.   char ch;
  197.   int ignore;
  198.   void *p;
  199.   int *ip;
  200.   int pos;
  201.  
  202.   va_start(args,format);
  203.   while( *f && *s ) {
  204.     ch=*f++;
  205.     if( ch!='%' ) {
  206.       if(isspace(ch)) {
  207.         /* match any number of whitespace */
  208.         while(isspace(*s)) s++;
  209.       } else {
  210.         /* match exactly the character ch */
  211.         if( *s!=ch ) goto finish;
  212.         s++;
  213.       }
  214.     } else {
  215.       /* we have got a '%' */
  216.       ch=*f++;
  217.       if( ch=='%' ) {
  218.         /* match exactly % */
  219.         if( *s!=ch ) goto finish;
  220.         s++;
  221.       } else {
  222.         f2=onefmt;
  223.         *f2++='%';
  224.         *f2++=ch;
  225.         ignore=0;
  226.         if( ch=='*' ) {
  227.           ignore=1;
  228.           ch=f2[-1]=*f++;
  229.         }
  230.         while( isdigit(ch) ) {
  231.           ch=*f2++=*f++;
  232.         }
  233.         if( ch=='l' || ch=='L' || ch=='h' ) {
  234.           ch=*f2++=*f++;
  235.         }
  236.         switch(ch) {
  237.           case '[':
  238.             while( ch && ch!=']' ) {
  239.               ch=*f2++=*f++;
  240.             }
  241.             if( !ch ) goto error;
  242.             break;
  243.           case 'e':
  244.           case 'f':
  245.           case 'g':
  246.           case 'd':
  247.           case 'o':
  248.           case 'i':
  249.           case 'u':
  250.           case 'x':
  251.           case 'c':
  252.           case 's':
  253.           case 'p':
  254.           case 'n': /* special case handled below */
  255.             break;
  256.           default:
  257.             goto error;
  258.         }
  259.         if( ch!='n' ) {
  260.           strcpy(f2,"%n");
  261.           if( ignore ) {
  262.             p=buffer;
  263.           } else {
  264.             p=va_arg(args,void *);
  265.           }
  266.           switch( sscanf( s, onefmt, p, &pos ) ) {
  267.             case EOF: goto error;
  268.             case  0 : goto finish;
  269.           }
  270.           if( !ignore ) cnt++;
  271.           s+=pos;
  272.         } else {
  273.           if( !ignore ) {
  274.             ip=va_arg(args,int *);
  275.             *ip=(int)(s-string);
  276.           }
  277.         }
  278.       }
  279.     }
  280.   }
  281.  
  282.   if( !*f ) goto finish;
  283.  
  284. error:
  285.   cnt=EOF;
  286. finish:
  287.   va_end(args);
  288.   return cnt;
  289. }
  290.  
  291. /* use the substitute now. I know this is dirty trick, but it works. */
  292. #define sscanf purec_sscanf
  293.  
  294. #endif /* __PUREC__ */
  295. #endif /* ATARI */
  296.  
  297. /*
  298.  * Only reference to contours library.
  299.  */
  300. extern struct gnuplot_contours *contour();
  301.  
  302. #ifdef OS2
  303.  /* emx has getcwd, chdir that can handle drive names */
  304. #define getcwd _getcwd2
  305. #define chdir  _chdir2
  306. #endif /* OS2 */
  307.  
  308. #if defined(unix) && !defined(hpux)
  309. #ifdef GETCWD
  310. extern char    *getcwd();    /* some Unix's use getcwd */
  311. #else
  312. extern char    *getwd();    /* most Unix's use getwd */
  313. #endif
  314. #else
  315. #ifdef DJGPP
  316. extern char    *getwd();    /* DJGPP acts like Unix here */
  317. #else
  318. extern char    *getcwd();    /* Turbo C, MSC, EMX, OS2 and VMS use getcwd */
  319. #endif
  320. #endif
  321.  
  322. #ifdef vms
  323. int             vms_vkid;    /* Virtual keyboard id */
  324. #endif
  325.     
  326. static FILE *data_fp=NULL;    /* != means file still open */
  327. static TBOOLEAN more_data_fp=FALSE;  /* And this explicitly says so. */
  328.  
  329. #if defined(unix) || defined(PIPES)
  330. extern FILE    *popen();
  331. static TBOOLEAN  pipe_open = FALSE;
  332. #endif
  333.  
  334. extern int      chdir();
  335.  
  336. extern double   magnitude(), angle(), real(), imag();
  337. extern struct value *const_express(), *pop(), *Gcomplex();
  338. extern struct at_type *temp_at(), *perm_at();
  339. extern struct udft_entry *add_udf();
  340. extern struct udvt_entry *add_udv();
  341. extern void     squash_spaces();
  342. extern void     lower_case();
  343.  
  344. /* local functions */
  345. static enum coord_type adjustlog();
  346.  
  347. extern TBOOLEAN  interactive;    /* from plot.c */
  348.  
  349. /* input data, parsing variables */
  350. struct lexical_unit token[MAX_TOKENS];
  351. char            input_line[MAX_LINE_LEN + 1] = "";
  352. int             num_tokens, c_token;
  353. int             inline_num = 0;    /* input line number */
  354.  
  355. char            c_dummy_var[MAX_NUM_VAR][MAX_ID_LEN + 1];    /* current dummy vars */
  356.  
  357. /* the curves/surfaces of the plot */
  358. struct curve_points *first_plot = NULL;
  359. struct surface_points *first_3dplot = NULL;
  360. static struct udft_entry plot_func;
  361. struct udft_entry *dummy_func;
  362.  
  363. /* jev -- for passing data thru user-defined function */
  364. static struct udft_entry ydata_func;
  365.  
  366. /* support for replot command */
  367. char            replot_line[MAX_LINE_LEN + 1] = "";
  368. static int      plot_token;    /* start of 'plot' command */
  369.  
  370. /* If last plot was a 3d one. */
  371. TBOOLEAN         is_3d_plot = FALSE;
  372.  
  373. com_line()
  374. {
  375.    if (read_line(PROMPT))
  376.        return(1);
  377.  
  378.     /* So we can flag any new output: if false at time of error, */
  379.     /* we reprint the command line before printing caret. */
  380.     /* TRUE for interactive terminals, since the command line is typed. */
  381.     /* FALSE for non-terminal stdin, so command line is printed anyway. */
  382.     /* (DFK 11/89) */
  383.     screen_ok = interactive;
  384.  
  385.     if (do_line())
  386.         return(1);
  387.      else
  388.         return(0);
  389. }
  390.  
  391.  
  392. do_line()
  393. {                /* also used in load_file */
  394.     if (is_system(input_line[0])) {
  395.     do_system();
  396.     (void) fputs("!\n", stderr);
  397.     return(0);
  398.     }
  399.     num_tokens = scanner(input_line);
  400.     c_token = 0;
  401.     while (c_token < num_tokens) {
  402.     if (command())
  403.            return(1);
  404.     if (c_token < num_tokens)    /* something after command */
  405.         if (equals(c_token, ";"))
  406.         c_token++;
  407.         else
  408.         int_error("';' expected", c_token);
  409.     }
  410.     return(0);
  411. }
  412.  
  413.  
  414.  
  415. command()
  416. {
  417.     FILE *fp, *lf_top();
  418.     int             i;
  419.     char            sv_file[MAX_LINE_LEN + 1];
  420. #if defined(__ZTC__)
  421.     unsigned dummy; /* it's a parameter needed for dos_setdrive */
  422. #endif
  423.     /* string holding name of save or load file */
  424.  
  425.     for (i = 0; i < MAX_NUM_VAR; i++)
  426.     c_dummy_var[i][0] = '\0';    /* no dummy variables */
  427.  
  428.     if (is_definition(c_token))
  429.     define();
  430.     else if (almost_equals(c_token, "h$elp") || equals(c_token, "?")) {
  431.     c_token++;
  432.     do_help();
  433.     } else if (almost_equals(c_token, "test")) {
  434.     c_token++;
  435.     test_term();
  436.     } else if (almost_equals(c_token, "scr$eendump")) {
  437.     c_token++;
  438. #ifdef _Windows
  439.     screen_dump();
  440. #else
  441.     fputs("screendump not implemented\n",stderr);
  442. #endif
  443.     } else if (almost_equals(c_token, "pa$use")) {
  444.     struct value    a;
  445.     int             stime, text = 0;
  446.     char            buf[MAX_LINE_LEN + 1];
  447.  
  448.     c_token++;
  449.     stime = (int) real(const_express(&a));
  450.     buf[0]='\0';
  451.     if (!(END_OF_COMMAND)) {
  452.         if (!isstring(c_token))
  453.         int_error("expecting string", c_token);
  454.         else {
  455.         quotel_str(buf, c_token);
  456. #ifdef _Windows
  457.         if (stime>=0)
  458. #endif
  459. #ifdef OS2
  460.                 if( strcmp(term_tbl[term].name, "pm" )!=0 || stime >=0 )
  461. #endif
  462.         (void) fprintf(stderr, "%s", buf);
  463.         text = 1;
  464.         }
  465.     }
  466.     if (stime < 0)
  467. #ifdef _Windows
  468.         {
  469.             if (!Pause(buf))
  470.                  longjmp(env, TRUE); /* bail out to command line */
  471.          }
  472. #else
  473. #ifdef OS2
  474.         if( strcmp(term_tbl[term].name, "pm" )==0 && stime < 0 )
  475.         {
  476.             int rc ;
  477.             if( (rc=PM_pause( buf ))==0 ) longjmp(env,TRUE) ;
  478.             else if( rc==2 ) { 
  479.         (void) fprintf(stderr, "%s", buf);
  480.         text = 1;
  481.                 (void) fgets(buf, MAX_LINE_LEN, stdin);
  482.                 }
  483.         }
  484. #else
  485.         (void) fgets(buf, MAX_LINE_LEN, stdin);
  486.     /* Hold until CR hit. */
  487. #endif /*OS2*/
  488. #endif
  489. #ifdef __ZTC__
  490.     if (stime > 0)
  491.         usleep((unsigned long) stime);
  492. #else
  493.     if (stime > 0)
  494.         sleep((unsigned int) stime);
  495. #endif
  496.     if (text != 0 && stime >= 0)
  497.         (void) fprintf(stderr, "\n");
  498.     c_token++;
  499.     screen_ok = FALSE;
  500.     } else if (almost_equals(c_token, "pr$int")) {
  501.     struct value    a;
  502.  
  503.     c_token++;
  504.     (void) const_express(&a);
  505.     (void) putc('\t', stderr);
  506.     disp_value(stderr, &a);
  507.     (void) putc('\n', stderr);
  508.     screen_ok = FALSE;
  509.     } else if (almost_equals(c_token, "p$lot")) {
  510.     plot_token = c_token++;
  511. #ifdef _Windows
  512.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_WAIT));
  513. #endif
  514.     plotrequest();
  515. #ifdef _Windows
  516.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_ARROW));
  517. #endif
  518.     } else if (almost_equals(c_token, "sp$lot")) {
  519.     plot_token = c_token++;
  520. #ifdef _Windows
  521.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_WAIT));
  522. #endif
  523.     plot3drequest();
  524. #ifdef _Windows
  525.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_ARROW));
  526. #endif
  527.     } else if (almost_equals(c_token, "rep$lot")) {
  528.     if (replot_line[0] == '\0')
  529.         int_error("no previous plot", c_token);
  530.     c_token++;
  531. #ifdef _Windows
  532.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_WAIT));
  533. #endif
  534.     replotrequest();
  535. #ifdef _Windows
  536.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_ARROW));
  537. #endif
  538.     } else if (almost_equals(c_token, "se$t"))
  539.     set_command();
  540.     else if (almost_equals(c_token, "sh$ow"))
  541.     show_command();
  542.     else if (almost_equals(c_token, "cl$ear")) {
  543.     if (!term_init) {
  544.         (*term_tbl[term].init) ();
  545.         term_init = TRUE;
  546.     }
  547.     (*term_tbl[term].graphics) ();
  548.     (*term_tbl[term].text) ();
  549.     (void) fflush(outfile);
  550.     screen_ok = FALSE;
  551.     c_token++;
  552.     } else if (almost_equals(c_token, "she$ll")) {
  553.     do_shell();
  554.     screen_ok = FALSE;
  555.     c_token++;
  556.     } else if (almost_equals(c_token, "sa$ve")) {
  557.     if (almost_equals(++c_token, "f$unctions")) {
  558.         if (!isstring(++c_token))
  559.         int_error("expecting filename", c_token);
  560.         else {
  561.         quote_str(sv_file, c_token);
  562.         save_functions(fopen(sv_file, "w"));
  563.         }
  564.     } else if (almost_equals(c_token, "v$ariables")) {
  565.         if (!isstring(++c_token))
  566.         int_error("expecting filename", c_token);
  567.         else {
  568.         quote_str(sv_file, c_token);
  569.         save_variables(fopen(sv_file, "w"));
  570.         }
  571.     } else if (almost_equals(c_token, "s$et")) {
  572.         if (!isstring(++c_token))
  573.         int_error("expecting filename", c_token);
  574.         else {
  575.         quote_str(sv_file, c_token);
  576.         save_set(fopen(sv_file, "w"));
  577.         }
  578.     } else if (isstring(c_token)) {
  579.         quote_str(sv_file, c_token);
  580.         save_all(fopen(sv_file, "w"));
  581.     } else {
  582.         int_error(
  583.              "filename or keyword 'functions', 'variables', or 'set' expected",
  584.              c_token);
  585.     }
  586.     c_token++;
  587.     } else if (almost_equals(c_token, "l$oad")) {
  588.     if (!isstring(++c_token))
  589.         int_error("expecting filename", c_token);
  590.     else {
  591.         quote_str(sv_file, c_token);
  592.         load_file(fp=fopen(sv_file, "r"), sv_file);
  593.         /* input_line[] and token[] now destroyed! */
  594.         c_token = num_tokens = 0;
  595.     }
  596.     } else if (almost_equals(c_token,"rer$ead")) {
  597.             fp = lf_top();
  598.             if (fp != (FILE *)NULL) rewind(fp);
  599.             c_token++;
  600.     } else if (almost_equals(c_token, "cd")) {
  601.     if (!isstring(++c_token))
  602.         int_error("expecting directory name", c_token);
  603.     else {
  604.         quotel_str(sv_file, c_token);
  605. #if defined(MSDOS) || defined(_Windows) || defined(ATARI) || defined(DOS386)
  606.         if (!((strlen(sv_file)==2) && isalpha(sv_file[0]) && (sv_file[1]==':')))
  607. #endif
  608.         if (chdir(sv_file)) {
  609.         int_error("Can't change to this directory", c_token);
  610.         }
  611. #if defined(MSDOS) || defined(_Windows) || defined(ATARI) || defined(DOS386)
  612.         if (isalpha(sv_file[0]) && (sv_file[1]==':')) {
  613. #ifdef ATARI
  614.         (void)Dsetdrv(toupper(sv_file[0])-'A');
  615. #endif
  616.  
  617. #if defined(__ZTC__)
  618.       (void)dos_setdrive(toupper(sv_file[0]) - 'A' + 1, &dummy);
  619. #endif
  620.  
  621. #if defined(MSDOS) && defined(__EMX__)
  622.         (void)_chdrive(toupper(sv_file[0]));
  623. #endif
  624. #if defined(__MSC__)
  625.         (void)_chdrive(toupper(sv_file[0])-'A');
  626. #endif
  627. #if (defined(MSDOS) || defined(_Windows)) && defined(__TURBOC__)
  628.         (void) setdisk(toupper(sv_file[0])-'A');
  629. #endif
  630. #ifdef DJGPP
  631.         { union REGS r;
  632.             r.h.ah = 0x0e;
  633.             r.x.dx = toupper(sv_file[0])-'A';
  634.             intdos(&r, &r);
  635.         }
  636. #endif
  637.         }
  638. #endif
  639.         c_token++;
  640.     }
  641.     } else if (almost_equals(c_token, "pwd")) {
  642. #if defined(unix) && !defined(hpux)
  643. #ifdef GETCWD
  644.     (void) getcwd(sv_file, MAX_ID_LEN);    /* some Unix's use getcwd */
  645. #else
  646.     (void) getwd(sv_file);    /* most Unix's use getwd */
  647. #endif
  648. #else
  649. #ifdef __EMX__
  650.     (void) _getcwd2(sv_file, MAX_ID_LEN);
  651. #else
  652.     /* Turbo C and VMS have getcwd() */
  653.     (void) getcwd(sv_file, MAX_ID_LEN);
  654. #endif
  655. #endif
  656. #ifdef DJGPP
  657.     { union REGS r;
  658.         r.h.ah = 0x19;
  659.         intdos(&r, &r);
  660.         fprintf(stderr, "%c:", r.h.al + 'a');
  661.     }
  662. #endif
  663.     fprintf(stderr, "%s\n", sv_file);
  664.     c_token++;
  665.     } else if (almost_equals(c_token, "ex$it") ||
  666.            almost_equals(c_token, "q$uit")) {
  667.     return(1);
  668.     } else if (!equals(c_token, ";")) {    /* null statement */
  669.     int_error("invalid command", c_token);
  670.     }
  671.     return(0);
  672. }
  673.  
  674. replotrequest()
  675. {
  676.     char            str[MAX_LINE_LEN + 1];
  677.     if (equals(c_token, "["))
  678.     int_error("cannot set range with replot", c_token);
  679.     if (!END_OF_COMMAND) {
  680.     capture(str, c_token, num_tokens - 1);
  681.     if ((strlen(str) + strlen(replot_line)) <= MAX_LINE_LEN - 1) {
  682.         (void) strcat(replot_line, ",");
  683.         (void) strcat(replot_line, str);
  684.     } else {
  685.         int_error("plot line too long with replot arguments", c_token);
  686.     }
  687.     }
  688.     (void) strcpy(input_line, replot_line);
  689.     screen_ok = FALSE;
  690.     num_tokens = scanner(input_line);
  691.     c_token = 1;        /* skip the 'plot' part */
  692.     is_3d_plot ? plot3drequest() : plotrequest();
  693. }
  694.  
  695.  
  696. plotrequest()
  697. /*
  698.  * In the parametric case we can say plot [a= -4:4] [-2:2] [-1:1] sin(a),a**2
  699.  * while in the non-parametric case we would say only plot [b= -2:2] [-1:1]
  700.  * sin(b)
  701.  */
  702. {
  703.     TBOOLEAN         changed;
  704.     int             dummy_token = -1;
  705.  
  706.     is_3d_plot = FALSE;
  707.  
  708.     if (parametric && strcmp(dummy_var[0], "u") == 0)
  709.     strcpy(dummy_var[0], "t");
  710.  
  711.     autoscale_lt = autoscale_t;
  712.     autoscale_lx = autoscale_x;
  713.     autoscale_ly = autoscale_y;
  714.  
  715.     if (!term)            /* unknown */
  716.     int_error("use 'set term' to set terminal type first", c_token);
  717.  
  718.     if (equals(c_token, "[")) {
  719.     c_token++;
  720.     if (isletter(c_token)) {
  721.         if (equals(c_token + 1, "=")) {
  722.         dummy_token = c_token;
  723.         c_token += 2;
  724.         } else {
  725.         /* oops; probably an expression with a variable. */
  726.         /* Parse it as an xmin expression. */
  727.         /* used to be: int_error("'=' expected",c_token); */
  728.         }
  729.     }
  730.     changed = parametric ? load_range(&tmin, &tmax) : load_range(&xmin, &xmax);
  731.     if (!equals(c_token, "]"))
  732.         int_error("']' expected", c_token);
  733.     c_token++;
  734.     if (changed) {
  735.         if (parametric)
  736.         autoscale_lt = FALSE;
  737.         else
  738.         autoscale_lx = FALSE;
  739.     }
  740.     }
  741.     if (parametric && equals(c_token, "[")) {    /* set optional x ranges */
  742.     c_token++;
  743.     changed = load_range(&xmin, &xmax);
  744.     if (!equals(c_token, "]"))
  745.         int_error("']' expected", c_token);
  746.     c_token++;
  747.     if (changed)
  748.         if(parametric)
  749.             autoscale_lt = FALSE;
  750.         else
  751.                 autoscale_lx = FALSE;
  752.     }
  753.     if (equals(c_token, "[")) {    /* set optional y ranges */
  754.     c_token++;
  755.     changed = load_range(&ymin, &ymax);
  756.     if (!equals(c_token, "]"))
  757.         int_error("']' expected", c_token);
  758.     c_token++;
  759.     if (changed)
  760.         autoscale_ly = FALSE;
  761.     }
  762.     /* use the default dummy variable unless changed */
  763.     if (dummy_token >= 0)
  764.     copy_str(c_dummy_var[0], dummy_token);
  765.     else
  766.     (void) strcpy(c_dummy_var[0], dummy_var[0]);
  767.  
  768.     eval_plots();
  769. }
  770.  
  771. plot3drequest()
  772. /*
  773.  * in the parametric case we would say splot [u= -Pi:Pi] [v= 0:2*Pi] [-1:1]
  774.  * [-1:1] [-1:1] sin(v)*cos(u),sin(v)*cos(u),sin(u) in the non-parametric
  775.  * case we would say only splot [x= -2:2] [y= -5:5] sin(x)*cos(y)
  776.  * 
  777.  */
  778. {
  779.     TBOOLEAN         changed;
  780.     int             dummy_token0 = -1, dummy_token1 = -1;
  781.  
  782.     is_3d_plot = TRUE;
  783.  
  784.     if (parametric && strcmp(dummy_var[0], "t") == 0) {
  785.     strcpy(dummy_var[0], "u");
  786.     strcpy(dummy_var[1], "v");
  787.     }
  788.     autoscale_lx = autoscale_x;
  789.     autoscale_ly = autoscale_y;
  790.     autoscale_lz = autoscale_z;
  791.  
  792.     if (!term)            /* unknown */
  793.     int_error("use 'set term' to set terminal type first", c_token);
  794.  
  795.     if (equals(c_token, "[")) {
  796.     c_token++;
  797.     if (isletter(c_token)) {
  798.         if (equals(c_token + 1, "=")) {
  799.         dummy_token0 = c_token;
  800.         c_token += 2;
  801.         } else {
  802.         /* oops; probably an expression with a variable. */
  803.         /* Parse it as an xmin expression. */
  804.         /* used to be: int_error("'=' expected",c_token); */
  805.         }
  806.     }
  807.     changed = parametric ? load_range(&umin, &umax) : load_range(&xmin, &xmax);
  808.     if (!equals(c_token, "]"))
  809.         int_error("']' expected", c_token);
  810.     c_token++;
  811.     if (changed)
  812.         if(parametric) 
  813.             autoscale_lu = FALSE;
  814.         else
  815.             autoscale_lx = FALSE;
  816.     }
  817.     if (equals(c_token, "[")) {
  818.     c_token++;
  819.     if (isletter(c_token)) {
  820.         if (equals(c_token + 1, "=")) {
  821.         dummy_token1 = c_token;
  822.         c_token += 2;
  823.         } else {
  824.         /* oops; probably an expression with a variable. */
  825.         /* Parse it as an xmin expression. */
  826.         /* used to be: int_error("'=' expected",c_token); */
  827.         }
  828.     }
  829.     changed = parametric ? load_range(&vmin, &vmax) : load_range(&ymin, &ymax);
  830.     if (!equals(c_token, "]"))
  831.         int_error("']' expected", c_token);
  832.     c_token++;
  833.     if (changed)
  834.         if(parametric) 
  835.             autoscale_lv = FALSE;
  836.         else
  837.             autoscale_ly = FALSE;
  838.     }
  839.     if (equals(c_token, "[")) {    /* set optional x (parametric) or z ranges */
  840.     c_token++;
  841.     changed = parametric ? load_range(&xmin, &xmax) : load_range(&zmin, &zmax);
  842.     if (!equals(c_token, "]"))
  843.         int_error("']' expected", c_token);
  844.     c_token++;
  845.     if (changed)
  846.         if(parametric) 
  847.             autoscale_lx = FALSE;
  848.         else
  849.             autoscale_lz = FALSE;
  850.     }
  851.     if (equals(c_token, "[")) {    /* set optional y ranges */
  852.     c_token++;
  853.     changed = load_range(&ymin, &ymax);
  854.     if (!equals(c_token, "]"))
  855.         int_error("']' expected", c_token);
  856.     c_token++;
  857.     if (changed)
  858.         autoscale_ly = FALSE;
  859.     }
  860.     if (equals(c_token, "[")) {    /* set optional z ranges */
  861.     c_token++;
  862.     changed = load_range(&zmin, &zmax);
  863.     if (!equals(c_token, "]"))
  864.         int_error("']' expected", c_token);
  865.     c_token++;
  866.     if (changed)
  867.         autoscale_lz = FALSE;
  868.     }
  869.     /* use the default dummy variable unless changed */
  870.     if (dummy_token0 >= 0)
  871.     copy_str(c_dummy_var[0], dummy_token0);
  872.     else
  873.     (void) strcpy(c_dummy_var[0], dummy_var[0]);
  874.  
  875.     if (dummy_token1 >= 0)
  876.     copy_str(c_dummy_var[1], dummy_token1);
  877.     else
  878.     (void) strcpy(c_dummy_var[1], dummy_var[1]);
  879.  
  880.     eval_3dplots();
  881. }
  882.  
  883.  
  884. define()
  885. {
  886.     register int    start_token;/* the 1st token in the function definition */
  887.     register struct udvt_entry *udv;
  888.     register struct udft_entry *udf;
  889.  
  890.     if (equals(c_token + 1, "(")) {
  891.     /* function ! */
  892.     int             dummy_num = 0;
  893.     start_token = c_token;
  894.     do {
  895.         c_token += 2;    /* skip to the next dummy */
  896.         copy_str(c_dummy_var[dummy_num++], c_token);
  897.     } while (equals(c_token + 1, ",") && (dummy_num < MAX_NUM_VAR));
  898.     if (equals(c_token + 1, ","))
  899.         int_error("function contains too many parameters", c_token + 2);
  900.     c_token += 3;        /* skip (, dummy, ) and = */
  901.     if (END_OF_COMMAND)
  902.         int_error("function definition expected", c_token);
  903.     udf = dummy_func = add_udf(start_token);
  904.     if (udf->at)        /* already a dynamic a.t. there */
  905.         free((char *) udf->at);    /* so free it first */
  906.     if ((udf->at = perm_at()) == (struct at_type *) NULL)
  907.         int_error("not enough memory for function", start_token);
  908.     m_capture(&(udf->definition), start_token, c_token - 1);
  909.     } else {
  910.     /* variable ! */
  911.     start_token = c_token;
  912.     c_token += 2;
  913.     udv = add_udv(start_token);
  914.     (void) const_express(&(udv->udv_value));
  915.     udv->udv_undef = FALSE;
  916.     }
  917. }
  918.  
  919. get_data(this_plot)
  920.     struct curve_points *this_plot;
  921. {
  922.     register int    i, j, l_num, datum;
  923.     int fcol[5], scol[5], ncol[5], prevmin, col;
  924.     char  format[MAX_LINE_LEN + 1], data_file[MAX_LINE_LEN + 1],
  925.           line[MAX_LINE_LEN + 1];
  926.     /* conversion variables */
  927.     int n, m, linestat, using;
  928.     char *s;
  929.     double val[5], v[5];
  930.     float fval[5];    /* for use in sscanf */
  931.  
  932.     /* close forgotten input file (in case of a syntax error) */
  933.     if( data_fp ) {
  934. #if defined(unix) || defined(PIPES)
  935.         if (pipe_open) {
  936.             (void) pclose(data_fp);
  937.             pipe_open = FALSE;
  938.         } else
  939. #endif /* unix || PIPES */
  940.         (void) fclose(data_fp);
  941.         data_fp=NULL;
  942.     }
  943.  
  944.     quotel_str(data_file, c_token);
  945.     this_plot->plot_type = DATA;
  946.     if (parametric)
  947.         int_error("Parametric data files not yet implemented", NO_CARET);
  948. #if defined(unix) || defined(PIPES)
  949.     if (*data_file == '<') {
  950.         if ((data_fp = popen(data_file + 1, "r")) == (FILE *) NULL)
  951.             os_error("cannot create pipe for data", c_token);
  952.         else
  953.             pipe_open = TRUE;
  954.     } else
  955. #endif /* unix || PIPES */
  956.     if ((data_fp = fopen(data_file, "r")) == (FILE *) NULL)
  957.         os_error("can't open data file", c_token);
  958.  
  959.     format[0] = '\0';
  960.     for (i=0; i<5; i++)
  961.         fcol[i] = i+1;
  962.  
  963.     using = 0;
  964.     c_token++;            /* skip data file name */
  965.  
  966.     /* jev -- support for passing data from file thru user function */
  967.     if (almost_equals(c_token, "thru$")) {
  968.         c_token++;
  969.         if (ydata_func.at)
  970.             free(ydata_func.at);
  971.         dummy_func = &ydata_func;
  972.         ydata_func.at = perm_at();
  973.     } else {
  974.         if (ydata_func.at)
  975.             free(ydata_func.at);
  976.         ydata_func.at = NULL;
  977.     }
  978.  
  979.     if (almost_equals(c_token,"u$sing")) {
  980.         using = 1;
  981.         c_token++;      /* skip "using" */
  982.             
  983.         if (!END_OF_COMMAND && !isstring(c_token)) {
  984.             struct value a;
  985.             for (i=0; i<5; i++)
  986.                 fcol[i] = -1;
  987.             fcol[0] = fcol[1] = (int)magnitude(const_express(&a));
  988.             for (i=1; equals(c_token,":") && i<5; i++) {
  989.                 c_token++;      /* skip ":" */
  990.                 fcol[i] = (int)magnitude(const_express(&a));
  991.             }
  992.             if (i==1)  /* only y column given */
  993.                 fcol[0] = 1;
  994.         }
  995.  
  996.         if (!END_OF_COMMAND && isstring(c_token)) {
  997.             quotel_str(format, c_token);
  998.             c_token++;    /* skip format */
  999.         }
  1000.     }
  1001.     
  1002.     /* sort fcol[] into scol[] removing duplicates */
  1003.     prevmin = 0;
  1004.     for (i=0; i<5; i++) {
  1005.         col = 10000;
  1006.         for (j=0; j<5; j++)
  1007.             if ((fcol[j]>prevmin) && (fcol[j]<col))
  1008.                 col = fcol[j];
  1009.         if (col <10000)
  1010.                prevmin = scol[i] = col;
  1011.         else
  1012.             scol[i] = 0;
  1013.     }
  1014.     /* normalise fcol[] into ncol[] */
  1015.     for (i=0; i<5; i++) {
  1016.         if (fcol[i] > 0)
  1017.             for (j=0; j<5; j++) {
  1018.                 if (fcol[i] == scol[j])
  1019.                     ncol[i] = j+1;
  1020.             }
  1021.         else if (fcol[i] == 0)
  1022.             ncol[i] = 0;
  1023.         else
  1024.             ncol[i] = -1;
  1025.     }
  1026.     /* set col to highest column number */
  1027.     col = 0;
  1028.     for (i=0; i<5; i++) 
  1029.         if (fcol[i]>col) col=fcol[i];
  1030.  
  1031.     l_num = 0;
  1032.     datum = 0;
  1033.     i = 0;
  1034.     while (fgets(line, MAX_LINE_LEN, data_fp) != (char *) NULL) {
  1035.         l_num++;
  1036.         if (is_comment(line[0]))
  1037.             continue;        /* ignore comments */
  1038.         if (i >= this_plot->p_max) {
  1039.             /*
  1040.              * overflow about to occur. Extend size of points[] array. We
  1041.              * either double the size, or add 1000 points, whichever is a
  1042.              * smaller increment. Note i=p_max.
  1043.              */
  1044.             cp_extend(this_plot, i + (i < 1000 ? i : 1000));
  1045.         }
  1046.         if (!line[1]) {        /* is it blank line ? */
  1047.             /* break in data, make next point undefined */
  1048.             this_plot->points[i].type = UNDEFINED;
  1049.             i++;
  1050.             continue;
  1051.         }
  1052.         if (strlen(format) != 0) {
  1053.             /* use old sscanf if a format string was given */
  1054.             m = sscanf(line, format, &fval[0], &fval[1], &fval[2], &fval[3], &fval[4]);
  1055.             for (n=0; n<5; n++)        /* convert floats from sscanf to double */
  1056.                 val[n] = (double)fval[n];
  1057.  
  1058.             for (j=0; j<5 && fcol[j]>=0 && fcol[j]-1<m ; j++)
  1059.                 if (fcol[j])
  1060.                     v[j] = val[fcol[j]-1];
  1061.                 else
  1062.                     v[j] = datum;    /* using 0:n */
  1063.         }
  1064.         else {
  1065.             /* implement our own sscanf that skips lines with invalid data 
  1066.              * if a using statement was given */
  1067.             /* convert the array to its constituents */
  1068.             for(n=0; n<5; n++)            /* wipe the array */
  1069.                 val[n] = 0.0;
  1070.             n=0;                        /* n is column number */
  1071.             m=0;                        /* m is number of values read */
  1072.             linestat = 1;                /* linestat: 1 OK 2 bad value 0 EOL */
  1073.             s = line;
  1074.             while ((linestat == 1) && (n<col)) {
  1075.                 while (isspace(*s)) s++;
  1076.                 if (*s == '\0') {
  1077.                     linestat = 0;
  1078.                     break;
  1079.                 }
  1080.                 n++;
  1081.                 if (n == scol[m]) {
  1082.                     if (isdigit(*s) || *s=='-' || *s=='+' || *s=='.') {
  1083.                         val[m] = atof(s);
  1084.                         m++;
  1085.                     }
  1086.                     else
  1087.                         linestat = 2;    /* abort the line non-digit in req loc */
  1088.                 }
  1089.                 while ((!isspace(*s)) && (*s != '\0')) s++;
  1090.             }
  1091.  
  1092.             if (using && (linestat == 2))
  1093.                 continue;    /* skip lines where a using pattern is present and not met */
  1094.  
  1095.             for (j=0; j<5 && ncol[j]>=0 && ncol[j]-1<m ; j++)
  1096.                 if (ncol[j])
  1097.                     v[j] = val[ncol[j]-1];
  1098.                 else
  1099.                     v[j] = datum;    /* using 0:n */
  1100.         }
  1101.  
  1102.         switch (j) {
  1103.             case 1: {        /* only one number */
  1104.                 /* x is index, assign number to y */
  1105.                 v[1]=v[0];
  1106.                 v[0]=datum;
  1107.                 /* nobreak */
  1108.             }
  1109.             case 2: {        /* x, y */
  1110.                 /* ylow and yhigh are same as y */
  1111.                 datum++;
  1112.                 store2d_point(this_plot, i++, v[0], v[1], v[1], v[1], -1.0);
  1113.                 break;
  1114.             }
  1115.             case 3: {        /* x, y, ydelta */
  1116.                 /* ydelta is in the ylow variable */
  1117.                 datum++;
  1118.                 store2d_point(this_plot, i++, v[0], v[1], v[1]-v[2], v[1]+v[2], -1.0);
  1119.                 break;
  1120.             }
  1121.             case 4: {        /* x, y, ylow, yhigh */
  1122.                 datum++;
  1123.                 store2d_point(this_plot, i++, v[0], v[1], v[2], v[3], -1.0);
  1124.                 break;
  1125.             }
  1126.             case 5: {        /* x, y, ylow, yhigh, width */
  1127.                 datum++;
  1128.                 store2d_point(this_plot, i++, v[0], v[1], v[2], v[3], v[4]);
  1129.                 break;
  1130.             }
  1131.             default: {
  1132.                 (void) sprintf(line, "bad data on line %d", l_num);
  1133.                 /* close file before exiting to command level */
  1134. #if defined(unix) || defined(PIPES)
  1135.                 if (pipe_open) {
  1136.                     (void) pclose(data_fp);
  1137.                     pipe_open = FALSE;
  1138.                 } else
  1139. #endif /* unix || PIPES */
  1140.                     (void) fclose(data_fp);
  1141.                 data_fp=NULL;
  1142.                 int_error(line, c_token);
  1143.             }
  1144.         }
  1145.     }
  1146.     this_plot->p_count = i;
  1147.     cp_extend(this_plot, i);    /* shrink to fit */
  1148.  
  1149. #if defined(unix) || defined(PIPES)
  1150.     if (pipe_open) {
  1151.         (void) pclose(data_fp);
  1152.         pipe_open = FALSE;
  1153.     } else
  1154. #endif /* unix || PIPES */
  1155.     (void) fclose(data_fp);
  1156.     data_fp=NULL;
  1157. }
  1158.  
  1159.  
  1160. /* called by get_data for each point */
  1161. store2d_point(this_plot, i, x, y, ylow, yhigh, width)
  1162.     struct curve_points *this_plot;
  1163.     int             i;        /* point number */
  1164.     double          x, y;
  1165.     double          ylow, yhigh;
  1166.     double          width;
  1167. {
  1168.     struct coordinate GPHUGE *cp = &(this_plot->points[i]);
  1169.  
  1170.     /* the easy part: */
  1171.     cp->type = INRANGE;
  1172.     cp->x = x;
  1173.     cp->y = y;
  1174.     cp->ylow = ylow;
  1175.     cp->yhigh = yhigh;
  1176.     cp->z = width;
  1177.  
  1178.     /* jev -- pass data values thru user-defined function */
  1179.     if (ydata_func.at) {
  1180.     struct value    val;
  1181.  
  1182.     (void) Gcomplex(&ydata_func.dummy_values[0], y, 0.0);
  1183.     evaluate_at(ydata_func.at, &val);
  1184.     cp->y = real(&val);
  1185.  
  1186.     (void) Gcomplex(&ydata_func.dummy_values[0], ylow, 0.0);
  1187.     evaluate_at(ydata_func.at, &val);
  1188.     cp->ylow = real(&val);
  1189.  
  1190.     (void) Gcomplex(&ydata_func.dummy_values[0], yhigh, 0.0);
  1191.     evaluate_at(ydata_func.at, &val);
  1192.     cp->yhigh = real(&val);
  1193.     }
  1194.     /* Adjust for log scale. */
  1195.     if (is_log_x) {
  1196.         cp->type = adjustlog(cp->type, &(cp->x), log_base_log_x);
  1197.         (void) adjustlog(cp->type, &(cp->z), log_base_log_z);
  1198.     }
  1199.     if (is_log_y) {
  1200.     cp->type = adjustlog(cp->type, &(cp->y), log_base_log_y);
  1201.     /* Note ylow,yhigh can't affect cp->type. */
  1202.     (void) adjustlog(cp->type, &(cp->ylow), log_base_log_y);
  1203.     (void) adjustlog(cp->type, &(cp->yhigh), log_base_log_y);
  1204.     }
  1205.     /* Now adjust the xrange, or declare the point out of range */
  1206.     /*
  1207.      * The yrange is handled later, once we know whether to include ylow,
  1208.      * yhigh in the calculation. See adjust_yrange()
  1209.      */
  1210.     if (cp->type == INRANGE)
  1211.     if (autoscale_lx || inrange(x, xmin, xmax)) {
  1212.         if (autoscale_lx) {
  1213.         if (x < xmin)
  1214.             xmin = x;
  1215.         if (x > xmax)
  1216.             xmax = x;
  1217.         }
  1218.     } else {
  1219.         cp->type = OUTRANGE;
  1220.     }
  1221. }
  1222.  
  1223.  
  1224. /*
  1225.  * Adjust for log scale: take the log of the second parameter, in place, if
  1226.  * possible. If not possible, return new type for point; if possible, then
  1227.  * return old type for point.  The log is taken to the base implicit in the
  1228.  * third parameter.
  1229.  */
  1230. static enum coord_type
  1231. adjustlog(type, val, log_base_log)
  1232.     enum coord_type type;
  1233.     coordval       *val;
  1234.     double          log_base_log;
  1235. {
  1236.     if (*val < 0.0) {
  1237.     return (UNDEFINED);
  1238.     } else if (*val == 0.0) {
  1239.     *val = -VERYLARGE;
  1240.     return (OUTRANGE);
  1241.     } else {
  1242.     *val = log(*val)/log_base_log;
  1243.     return (type);
  1244.     }
  1245. }
  1246.  
  1247.  
  1248. /* now adjust the yrange, or declare the point out of range */
  1249. /* this does all points in a curve */
  1250. adjust_yrange(curve)
  1251.     struct curve_points *curve;
  1252. {
  1253.     TBOOLEAN         ebars = (curve->plot_style == ERRORBARS);
  1254.     int             npoints = curve->p_count;    /* number of points */
  1255.     coordval        y, ylow, yhigh;    /* one point value */
  1256.     struct coordinate GPHUGE *cp;    /* one coordinate */
  1257.     int             i;        /* index into points */
  1258.  
  1259.     for (i = 0; i < npoints; i++) {
  1260.     cp = &(curve->points[i]);
  1261.     if (cp->type == INRANGE) {
  1262.         y = is_log_y ? pow(base_log_y, cp->y) : cp->y;
  1263.         if ((autoscale_ly ||
  1264.         /*
  1265.          * inrange((is_log_y ? pow(base_log_y, y) : y), ymin, ymax) ||
  1266.          */
  1267.          inrange((y), ymin, ymax) ||
  1268.          polar)) {
  1269.         if (autoscale_ly) {
  1270.             if (y < ymin)
  1271.             ymin = y;
  1272.             if (y > ymax)
  1273.             ymax = y;
  1274.             if (ebars) {
  1275.             ylow = is_log_y ? pow(base_log_y, cp->ylow) : cp->ylow;
  1276.             yhigh = is_log_y ? pow(base_log_y, cp->yhigh) : cp->yhigh;
  1277.             if (ylow < ymin)
  1278.                 ymin = ylow;
  1279.             if (ylow > ymax)
  1280.                 ymax = ylow;
  1281.             if (yhigh < ymin)
  1282.                 ymin = yhigh;
  1283.             if (yhigh > ymax)
  1284.                 ymax = yhigh;
  1285.             }
  1286.         }
  1287.         } else {
  1288.         cp->type = OUTRANGE;
  1289.         }
  1290.     }
  1291.     }
  1292. }
  1293.  
  1294. grid_nongrid_data(this_plot)
  1295. struct surface_points *this_plot;
  1296. {
  1297.     int i, j, k;
  1298.     double x, y, z, w, dx, dy, xmin, xmax, ymin, ymax;
  1299.     struct iso_curve *old_iso_crvs = this_plot->iso_crvs;
  1300.     struct iso_curve *icrv, *icrvs, *oicrv, *oicrvs;
  1301.  
  1302.     /* Compute XY bounding box on the original data. */
  1303.     xmin = xmax = old_iso_crvs->points[0].x;
  1304.     ymin = ymax = old_iso_crvs->points[0].y;
  1305.     for (icrv = old_iso_crvs; icrv != NULL; icrv = icrv->next) {
  1306.     struct coordinate GPHUGE *points = icrv->points;
  1307.  
  1308.     for (i = 0; i < icrv->p_count; i++, points++) {
  1309.         if (xmin > points->x)
  1310.         xmin = points->x;
  1311.         if (xmax < points->x)
  1312.         xmax = points->x;
  1313.         if (ymin > points->y)
  1314.         ymin = points->y;
  1315.         if (ymax < points->y)
  1316.         ymax = points->y;
  1317.     }
  1318.     }
  1319.  
  1320.     dx = (xmax - xmin) / (dgrid3d_row_fineness - 1);
  1321.     dy = (ymax - ymin) / (dgrid3d_row_fineness - 1);
  1322.  
  1323.     /* Create the new grid structure, and compute the low pass filtering from
  1324.      * non grid to grid structure.
  1325.      */
  1326.     this_plot->iso_crvs = NULL;
  1327.     this_plot->num_iso_read = dgrid3d_col_fineness;
  1328.     this_plot->has_grid_topology = TRUE;
  1329.     for (i = 0, x = xmin; i < dgrid3d_col_fineness; i++, x += dx) {
  1330.     struct coordinate GPHUGE *points;
  1331.  
  1332.     icrv = iso_alloc(dgrid3d_row_fineness + 1);
  1333.     icrv->p_count = dgrid3d_row_fineness;
  1334.     icrv->next = this_plot->iso_crvs;
  1335.     this_plot->iso_crvs = icrv;
  1336.     points = icrv->points;
  1337.  
  1338.     for (j = 0, y = ymin; j < dgrid3d_row_fineness; j++, y += dy, points++) {
  1339.         z = w = 0.0;
  1340.  
  1341.         for (oicrv = old_iso_crvs; oicrv != NULL; oicrv = oicrv->next) {
  1342.         struct coordinate GPHUGE *opoints = oicrv->points;
  1343.         for (k = 0; k < oicrv->p_count; k++, opoints++) {
  1344.             double dist,
  1345.                dist_x = fabs( opoints->x - x ),
  1346.                dist_y = fabs( opoints->y - y );
  1347.  
  1348.             switch (dgrid3d_norm_value) {
  1349.             case 1:
  1350.                 dist = dist_x + dist_y;
  1351.                 break;
  1352.             case 2:
  1353.                 dist = dist_x * dist_x + dist_y * dist_y;
  1354.                 break;
  1355.             case 4:
  1356.                 dist = dist_x * dist_x + dist_y * dist_y;
  1357.                 dist *= dist;
  1358.                 break;
  1359.             case 8:
  1360.                 dist = dist_x * dist_x + dist_y * dist_y;
  1361.                 dist *= dist;
  1362.                 dist *= dist;
  1363.                 break;
  1364.             case 16:
  1365.                 dist = dist_x * dist_x + dist_y * dist_y;
  1366.                 dist *= dist;
  1367.                 dist *= dist;
  1368.                 dist *= dist;
  1369.                 break;
  1370.             default:
  1371.                 dist = pow( dist_x, dgrid3d_norm_value ) +
  1372.                    pow( dist_y, dgrid3d_norm_value );
  1373.                 break;
  1374.             }
  1375.  
  1376.             /* The weight of this point is inverse proportional
  1377.              * to the distance.
  1378.              */
  1379.             if ( dist == 0.0 )
  1380. #ifndef AMIGA_SC_6_1
  1381.             dist = VERYLARGE;
  1382. #else /* AMIGA_SC_6_1 */
  1383.             /* Multiplying VERYLARGE by opoints->z below
  1384.              * might yield Inf (i.e. a number that can't
  1385.              * be represented on the machine). This will
  1386.              * result in points->z being set to NaN. It's
  1387.              * better to have a pretty large number that is
  1388.              * also on the safe side... The numbers that are
  1389.              * read by gnuplot are float values anyway, so
  1390.              * they can't be bigger than FLT_MAX. So setting
  1391.              * dist to FLT_MAX^2 will make dist pretty large
  1392.              * with respect to any value that has been read. */
  1393.             dist = ((double)FLT_MAX)*((double)FLT_MAX);
  1394. #endif /* AMIGA_SC_6_1 */
  1395.             else
  1396.             dist = 1.0 / dist;
  1397.  
  1398.             z += opoints->z * dist;
  1399.             w += dist;
  1400.         }
  1401.         }
  1402.  
  1403.         points->x = x;
  1404.         points->y = y;
  1405.         points->z = z / w;
  1406.         points->type = INRANGE;
  1407.     }
  1408.     }
  1409.     
  1410.     /* Delete the old non grid data. */
  1411.     for (oicrvs = old_iso_crvs; oicrvs != NULL;) {
  1412.     oicrv = oicrvs;
  1413.     oicrvs = oicrvs->next;
  1414.     iso_free(oicrv);
  1415.     }
  1416. }
  1417.  
  1418. get_3ddata(this_plot)
  1419.     struct surface_points *this_plot;
  1420. {
  1421.     register int    i, j, l_num, xdatum, ydatum;
  1422.     float           x, y, z;
  1423.     char            data_file[MAX_LINE_LEN + 1], line[MAX_LINE_LEN + 1];
  1424.     char           *float_format = "%f", *float_skip = "%*f";
  1425.     static TBOOLEAN  only_z = FALSE, using_format = FALSE;
  1426.     static int      xcol = 1, ycol = 2, zcol = 3, index = -1;
  1427.     static char     format[MAX_LINE_LEN + 1];
  1428.     int            pt_in_iso_crv = 0, maxcol;
  1429.     enum XYZ_order_type {
  1430.     XYZ, YXZ, ZXY, XZY, ZYX, YZX, XY, YX
  1431.     }               xyz_order;
  1432.     struct iso_curve *this_iso;
  1433.  
  1434.     /* close forgotten file (in case of a syntax error) */
  1435.     if (data_fp && !more_data_fp) {
  1436. #if defined(unix) || defined(PIPES)
  1437.     if (pipe_open) {
  1438.         (void) pclose(data_fp);
  1439.         pipe_open = FALSE;
  1440.     } else
  1441. #endif /* unix || PIPES */
  1442.         (void) fclose(data_fp);
  1443.         data_fp=NULL;
  1444.         }
  1445.  
  1446.     quotel_str(data_file, c_token);
  1447.     this_plot->plot_type = DATA3D;
  1448.     this_plot->has_grid_topology = TRUE;
  1449.     if (!more_data_fp) {
  1450. #if defined(unix) || defined(PIPES)
  1451.     if (*data_file == '<') {
  1452.         if ((data_fp = popen(data_file + 1, "r")) == (FILE *) NULL)
  1453.         os_error("cannot create pipe; output not changed", c_token);
  1454.         else
  1455.         pipe_open = TRUE;
  1456.     } else
  1457. #endif /* unix || PIPES */
  1458.         if ((data_fp = fopen(data_file, "r")) == (FILE *) NULL)
  1459.         os_error("can't open data file", c_token);
  1460.  
  1461.     /* Initialize defualt values. */
  1462.     only_z = FALSE;
  1463.     using_format = FALSE;
  1464.     xcol = 1;
  1465.     ycol = 2;
  1466.     zcol = 3;
  1467.     index = -1;
  1468.     format[0] = '\0';
  1469.  
  1470.     c_token++;            /* skip data file name */
  1471.     if (almost_equals(c_token, "i$ndex")) {
  1472.         struct value a;
  1473.         c_token++;        /* skip "index" */
  1474.         index = (int) magnitude(const_express(&a));
  1475.     }
  1476.     if (almost_equals(c_token, "u$sing")) {
  1477.         c_token++;        /* skip "using" */
  1478.         if (!END_OF_COMMAND && !isstring(c_token)) {
  1479.         struct value a;
  1480.         zcol = (int) magnitude(const_express(&a));
  1481.         only_z = TRUE;
  1482.         if (equals(c_token, ":")) {
  1483.             c_token++;    /* skip ":" */
  1484.             only_z = FALSE;
  1485.             ycol = zcol;
  1486.             zcol = (int) magnitude(const_express(&a));
  1487.             if (equals(c_token, ":")) {
  1488.             c_token++;    /* skip ":" */
  1489.             xcol = ycol;
  1490.             ycol = zcol;
  1491.             zcol = (int) magnitude(const_express(&a));
  1492.             } else {
  1493.             if (mapping3d == MAP3D_CARTESIAN)
  1494.                 int_error("Must specify 1 or 3 columns", c_token);
  1495.             xcol = ycol;
  1496.             ycol = zcol;
  1497.             }
  1498.         }
  1499.         if (!only_z)
  1500.             if ((xcol == ycol) || (ycol == zcol) || (xcol == zcol))
  1501.             int_error("Columns must be distinct", c_token);
  1502.         }
  1503.         if (!END_OF_COMMAND && isstring(c_token)) {
  1504.         quotel_str(format, c_token);
  1505.         using_format = TRUE;
  1506.         c_token++;        /* skip format */
  1507.         }
  1508.     } else {
  1509.         if ( (only_z = !parametric) != FALSE)
  1510.         zcol = 1;
  1511.     }
  1512.     }
  1513.  
  1514.     switch (mapping3d) {
  1515.     case MAP3D_CARTESIAN:
  1516.     maxcol = (xcol > ycol) ? xcol : ycol;
  1517.     maxcol = (maxcol > zcol) ? maxcol : zcol;
  1518.     if (!only_z) {        /* Determine ordering of input columns */
  1519.         if (zcol == maxcol) {
  1520.         if (xcol < ycol)
  1521.             xyz_order = XYZ;    /* scanf(x,y,z) */
  1522.         else
  1523.             xyz_order = YXZ;    /* scanf(y,x,z) */
  1524.         } else if (ycol == maxcol) {
  1525.         if (xcol < zcol)
  1526.             xyz_order = XZY;    /* scanf(x,z,y) */
  1527.         else
  1528.             xyz_order = ZXY;    /* scanf(z,x,y) */
  1529.         } else {
  1530.         if (ycol < zcol)
  1531.             xyz_order = YZX;    /* scanf(y,z,x) */
  1532.         else
  1533.             xyz_order = ZYX;    /* scanf(z,y,x) */
  1534.         }
  1535.     }
  1536.     if (strlen(format) == 0) {
  1537.         if (only_z) {
  1538.         for (i = 1; i <= zcol; i++)
  1539.             if (i == zcol)
  1540.             (void) strcat(format, float_format);
  1541.             else
  1542.             (void) strcat(format, float_skip);
  1543.         } else {
  1544.         for (i = 1; i <= maxcol; i++)
  1545.             if ((i == xcol) || (i == ycol) || (i == zcol))
  1546.             (void) strcat(format, float_format);
  1547.             else
  1548.             (void) strcat(format, float_skip);
  1549.         }
  1550.     }
  1551.     break;
  1552.     case MAP3D_SPHERICAL:
  1553.     case MAP3D_CYLINDRICAL:
  1554.     if (only_z)
  1555.         int_error("Two columns for spherical/cylindrical coords.", c_token);
  1556.     maxcol = (xcol > ycol) ? xcol : ycol;
  1557.     xyz_order = (xcol < ycol) ? XY : YX;
  1558.     for (i = 1; i <= maxcol; i++)
  1559.         if ((i == xcol) || (i == ycol))
  1560.         (void) strcat(format, float_format);
  1561.         else
  1562.         (void) strcat(format, float_skip);
  1563.     }
  1564.  
  1565.     l_num = 0;
  1566.     xdatum = 0;
  1567.     ydatum = 0;
  1568.     this_plot->num_iso_read = 0;
  1569.     this_plot->has_grid_topology = TRUE;
  1570.     if (this_plot->iso_crvs != NULL) {
  1571.     struct iso_curve *icrv, *icrvs = this_plot->iso_crvs;
  1572.  
  1573.     while (icrvs) {
  1574.         icrv = icrvs;
  1575.         icrvs = icrvs->next;
  1576.         iso_free(icrv);
  1577.     }
  1578.     this_plot->iso_crvs = NULL;
  1579.     }
  1580.     if (!more_data_fp && is_binary_file(data_fp)) {    /* MOD--RKC */
  1581. #if defined(MSDOS)||defined(ATARI)||defined(OS2)||defined(_Windows)||defined(DOS386)
  1582.     /* file must be opened with binary flag. the old cr/lf problem again */
  1583. #ifdef PIPES
  1584.     if( pipe_open ) {
  1585.       pclose(data_fp);
  1586.       data_fp=NULL;
  1587.       pipe_open=FALSE;
  1588.       int_error("binary data from pipes is not implemented", NO_CARET);
  1589.     }
  1590. #endif
  1591.     data_fp = freopen(data_file, "rb", data_fp);
  1592. #endif
  1593.     xdatum = get_binary_data(this_plot, data_fp, &this_iso);
  1594.     } else {
  1595.     int last_line_blank = FALSE;
  1596.  
  1597.     more_data_fp = FALSE;
  1598.  
  1599.     this_iso = iso_alloc(samples);
  1600.  
  1601.     if (index > 0) { /* Skip data meshes until mesh index is reached. */
  1602.         int i = index;
  1603.  
  1604.         while (i--) {
  1605.         while (fgets(line, MAX_LINE_LEN, data_fp) != (char *) NULL) {
  1606.             l_num++;
  1607.  
  1608.             if (!line[1]) {    /* is it blank line ? */
  1609.             if (last_line_blank) /* Two consecutive blanks. */
  1610.                 break;
  1611.             last_line_blank = TRUE;
  1612.             }
  1613.             else
  1614.             last_line_blank = FALSE;
  1615.         }
  1616.         if (feof(data_fp))
  1617.             int_error("mesh index overflow", NO_CARET);
  1618.         }
  1619.     }   /* end of index skip */
  1620.     
  1621.     last_line_blank = FALSE;
  1622.  
  1623.     while (fgets(line, MAX_LINE_LEN, data_fp) != (char *) NULL) {
  1624.         l_num++;
  1625.         if (is_comment(line[0]))
  1626.         continue;    /* ignore comments */
  1627.         if (!line[1]) {    /* is it blank line ? */
  1628.         if (last_line_blank) { /* Two consecutive blank lines. */
  1629.             more_data_fp = TRUE;
  1630.             break;
  1631.         }
  1632.         last_line_blank = TRUE;
  1633.  
  1634.         if (pt_in_iso_crv == 0) {
  1635.             if (xdatum == 0)
  1636.             continue;
  1637.             pt_in_iso_crv = xdatum;
  1638.         }
  1639.         if (xdatum > 0) {
  1640.             this_iso->p_count = xdatum;
  1641.             this_iso->next = this_plot->iso_crvs;
  1642.             this_plot->iso_crvs = this_iso;
  1643.             this_plot->num_iso_read++;
  1644.  
  1645.             if (xdatum != pt_in_iso_crv)
  1646.             this_plot->has_grid_topology = FALSE;
  1647.  
  1648.             this_iso = iso_alloc(pt_in_iso_crv);
  1649.             xdatum = 0;
  1650.             ydatum++;
  1651.         }
  1652.         continue;
  1653.         }
  1654.         last_line_blank = FALSE;
  1655.         if (xdatum >= this_iso->p_max) {
  1656.         /*
  1657.          * overflow about to occur. Extend size of points[] array. We
  1658.          * either double the size, or add 1000 points, whichever is a
  1659.          * smaller increment. Note i=p_max.
  1660.          */
  1661.         iso_extend(this_iso,
  1662.                xdatum + (xdatum < 1000 ? xdatum : 1000));
  1663.         }
  1664.         switch (sscanf(line, format, &x, &y, &z)) {
  1665.         case 3:        /* All parameter are specified. */
  1666.         if (!only_z || using_format) {
  1667.             switch (xyz_order) {
  1668.             case XYZ:    /* scanf(x,y,z) */
  1669.             this_iso->points[xdatum].x = x;
  1670.             this_iso->points[xdatum].y = y;
  1671.             this_iso->points[xdatum].z = z;
  1672.             break;
  1673.             case XZY:    /* scanf(x,z,y) */
  1674.             this_iso->points[xdatum].x = x;
  1675.             this_iso->points[xdatum].y = z;
  1676.             this_iso->points[xdatum].z = y;
  1677.             break;
  1678.             case YXZ:    /* scanf(y,x,z) */
  1679.             this_iso->points[xdatum].x = y;
  1680.             this_iso->points[xdatum].y = x;
  1681.             this_iso->points[xdatum].z = z;
  1682.             break;
  1683.             case ZXY:    /* scanf(z,x,y) */
  1684.             this_iso->points[xdatum].x = y;
  1685.             this_iso->points[xdatum].y = z;
  1686.             this_iso->points[xdatum].z = x;
  1687.             break;
  1688.             case YZX:    /* scanf(y,z,x) */
  1689.             this_iso->points[xdatum].x = z;
  1690.             this_iso->points[xdatum].y = x;
  1691.             this_iso->points[xdatum].z = y;
  1692.             break;
  1693.             case ZYX:    /* scanf(z,y,x) */
  1694.             this_iso->points[xdatum].x = z;
  1695.             this_iso->points[xdatum].y = y;
  1696.             this_iso->points[xdatum].z = x;
  1697.             break;
  1698.             }
  1699.             if (xyz_order != XYZ) {
  1700.             x = this_iso->points[xdatum].x;
  1701.             y = this_iso->points[xdatum].y;
  1702.             z = this_iso->points[xdatum].z;
  1703.             }
  1704.             if (!parametric)
  1705.             int_error("Must be in parametric mode.",
  1706.                   NO_CARET);
  1707.             break;
  1708.         }
  1709.         case 1:        /* only one number on the line */
  1710.         if (!only_z && !using_format)
  1711.             int_error("3 columns expected, only 1 found", c_token);
  1712.         /* assign that number to z */
  1713.         this_iso->points[xdatum].z = x;
  1714.         z = x;
  1715.         this_iso->points[xdatum].x = xdatum;
  1716.         x = this_iso->points[xdatum].x;
  1717.         this_iso->points[xdatum].y = ydatum;
  1718.         y = this_iso->points[xdatum].y;
  1719.         if (parametric)
  1720.             int_error("Must be in non parametric mode.",
  1721.                   NO_CARET);
  1722.         break;
  1723.         case 2:
  1724.         switch (xyz_order) {
  1725.         case YX:
  1726.             z = x;    /* Use z as temp */
  1727.             x = y;
  1728.             y = z;
  1729.             break;
  1730.         default:
  1731.             break;
  1732.         }
  1733.         switch (mapping3d) {
  1734.         case MAP3D_CARTESIAN:
  1735.             int_error("2 columns found, 1 or 3 expected",
  1736.                   c_token);
  1737.             break;
  1738.         case MAP3D_SPHERICAL:
  1739.             if (angles_format == ANGLES_DEGREES) {
  1740.             x *= DEG2RAD;    /* Convert to radians. */
  1741.             y *= DEG2RAD;
  1742.             }
  1743.             this_iso->points[xdatum].x = cos(x) * cos(y);
  1744.             this_iso->points[xdatum].y = sin(x) * cos(y);
  1745.             this_iso->points[xdatum].z = sin(y);
  1746.             break;
  1747.         case MAP3D_CYLINDRICAL:
  1748.             if (angles_format == ANGLES_DEGREES)
  1749.             x *= DEG2RAD;    /* Convert to radians. */
  1750.             this_iso->points[xdatum].x = cos(x);
  1751.             this_iso->points[xdatum].y = sin(x);
  1752.             this_iso->points[xdatum].z = y;
  1753.             break;
  1754.         }
  1755.         x = this_iso->points[xdatum].x;
  1756.         y = this_iso->points[xdatum].y;
  1757.         z = this_iso->points[xdatum].z;
  1758.         break;
  1759.         default:
  1760.         (void) sprintf(line, "bad data on line %d", l_num);
  1761.         int_error(line, c_token);
  1762.         }
  1763.  
  1764.         if (is_log_x) {
  1765.         if (x <= 0.0)
  1766.             int_error("X value must be above 0 for log scale!",
  1767.                   NO_CARET);
  1768.         else
  1769.             this_iso->points[xdatum].x =
  1770.             log(this_iso->points[xdatum].x)/log_base_log_x;
  1771.         }
  1772.         if (is_log_y) {
  1773.         if (y <= 0.0)
  1774.             int_error("Y value must be above 0 for log scale!",
  1775.                   NO_CARET);
  1776.         else
  1777.             this_iso->points[xdatum].y =
  1778.             log(this_iso->points[xdatum].y)/log_base_log_y;
  1779.         }
  1780.         if (is_log_z) {
  1781.         if (z <= 0.0)
  1782.             int_error("Z value must be above 0 for log scale!",
  1783.                   NO_CARET);
  1784.         else
  1785.             this_iso->points[xdatum].z =
  1786.             log(this_iso->points[xdatum].z)/log_base_log_z;
  1787.         }
  1788.         if (autoscale_lx) {
  1789.         if (x < xmin)
  1790.             xmin = x;
  1791.         if (x > xmax)
  1792.             xmax = x;
  1793.         }
  1794.         if (autoscale_ly) {
  1795.         if (y < ymin)
  1796.             ymin = y;
  1797.         if (y > ymax)
  1798.             ymax = y;
  1799.         }
  1800.         if (autoscale_lz) {
  1801.         if (z < zmin)
  1802.             zmin = z;
  1803.         if (z > zmax)
  1804.             zmax = z;
  1805.         }
  1806.         xdatum++;    
  1807.     }  /* end of while loop */
  1808.  
  1809.     if (xdatum > 0) {
  1810.         this_plot->num_iso_read++;    /* Update last iso. */
  1811.         this_iso->p_count = xdatum;
  1812.  
  1813.         this_iso->next = this_plot->iso_crvs;
  1814.         this_plot->iso_crvs = this_iso;
  1815.  
  1816.         if (xdatum != pt_in_iso_crv)
  1817.         this_plot->has_grid_topology = FALSE;
  1818.  
  1819.     } else {
  1820.         iso_free(this_iso);    /* Free last allocation. */
  1821.     }
  1822.     }                /* MOD-RKC else of binary */
  1823.  
  1824.     if (index >= 0) more_data_fp = FALSE; /* Only one data set please. */
  1825.  
  1826.     if (!more_data_fp) {
  1827.       if (this_plot->num_iso_read <= 1)
  1828.       this_plot->has_grid_topology = FALSE;
  1829.       if (this_plot->has_grid_topology && !hidden3d) {
  1830. #if defined(unix) || defined(PIPES)
  1831.     if (pipe_open) {
  1832.         (void) pclose(data_fp);
  1833.         pipe_open = FALSE;
  1834.     } else 
  1835. #endif /* unix || PIPES */
  1836.         (void) fclose(data_fp);  
  1837.             /* data file is not closed if hidden is true */
  1838.         data_fp = NULL;
  1839.         }
  1840.     }
  1841.  
  1842.     if (dgrid3d) grid_nongrid_data(this_plot);
  1843.  
  1844.     if (this_plot->num_iso_read <= 1)
  1845.     this_plot->has_grid_topology = FALSE;
  1846.     if (this_plot->has_grid_topology && !hidden3d) {
  1847.     struct iso_curve *new_icrvs = NULL;
  1848.     int             num_new_iso = this_plot->iso_crvs->p_count, len_new_iso = this_plot->num_iso_read;
  1849.  
  1850.     /* Now we need to set the other direction (pseudo) isolines. */
  1851.     for (i = 0; i < num_new_iso; i++) {
  1852.         struct iso_curve *new_icrv = iso_alloc(len_new_iso);
  1853.  
  1854.         new_icrv->p_count = len_new_iso;
  1855.  
  1856.         for (j = 0, this_iso = this_plot->iso_crvs;
  1857.          this_iso != NULL;
  1858.          j++, this_iso = this_iso->next) {
  1859.         new_icrv->points[j].x = this_iso->points[i].x;
  1860.         new_icrv->points[j].y = this_iso->points[i].y;
  1861.         new_icrv->points[j].z = this_iso->points[i].z;
  1862.         }
  1863.  
  1864.         new_icrv->next = new_icrvs;
  1865.         new_icrvs = new_icrv;
  1866.     }
  1867.  
  1868.     /* Append the new iso curves after the read ones. */
  1869.     for (this_iso = this_plot->iso_crvs;
  1870.          this_iso->next != NULL;
  1871.          this_iso = this_iso->next);
  1872.         this_iso->next = new_icrvs;
  1873.     }
  1874. }
  1875.  
  1876. /*
  1877.  * print_points: a debugging routine to print out the points of a curve, and
  1878.  * the curve structure. If curve<0, then we print the list of curves.
  1879.  */
  1880.  
  1881. static char    *plot_type_names[4] =
  1882. {
  1883.     "Function", "Data", "3D Function", "3d data"
  1884. };
  1885. static char    *plot_style_names[6] =
  1886. {
  1887.     "Lines", "Points", "Impulses", "LinesPoints", "Dots", "Errorbars"
  1888. };
  1889.  
  1890. print_points(curve)
  1891.     int             curve;    /* which curve to print */
  1892. {
  1893.     register struct curve_points *this_plot;
  1894.     int             i;
  1895.  
  1896.     if (curve < 0) {
  1897.     for (this_plot = first_plot, i = 0;
  1898.          this_plot != NULL;
  1899.          i++, this_plot = this_plot->next_cp) {
  1900.         printf("Curve %d:\n", i);
  1901.         if ((int) this_plot->plot_type >= 0 && (int) (this_plot->plot_type) < 4)
  1902.         printf("Plot type %d: %s\n", (int) (this_plot->plot_type),
  1903.                plot_type_names[(int) (this_plot->plot_type)]);
  1904.         else
  1905.         printf("Plot type %d: BAD\n", (int) (this_plot->plot_type));
  1906.         if ((int) this_plot->plot_style >= 0 && (int) (this_plot->plot_style) < 6)
  1907.         printf("Plot style %d: %s\n", (int) (this_plot->plot_style),
  1908.                plot_style_names[(int) (this_plot->plot_style)]);
  1909.         else
  1910.         printf("Plot style %d: BAD\n", (int) (this_plot->plot_style));
  1911.         printf("Plot title: '%s'\n", this_plot->title);
  1912.         printf("Line type %d\n", this_plot->line_type);
  1913.         printf("Point type %d\n", this_plot->point_type);
  1914.         printf("max points %d\n", this_plot->p_max);
  1915.         printf("current points %d\n", this_plot->p_count);
  1916.         printf("\n");
  1917.     }
  1918.     } else {
  1919.     for (this_plot = first_plot, i = 0;
  1920.          i < curve && this_plot != NULL;
  1921.          i++, this_plot = this_plot->next_cp);
  1922.     if (this_plot == NULL)
  1923.         printf("Curve %d does not exist; list has %d curves\n", curve, i);
  1924.     else {
  1925.         printf("Curve %d, %d points\n", curve, this_plot->p_count);
  1926.         for (i = 0; i < this_plot->p_count; i++) {
  1927.         printf("%c x=%g y=%g z=%g ylow=%g yhigh=%g\n",
  1928.                this_plot->points[i].type == INRANGE ? 'i'
  1929.                : this_plot->points[i].type == OUTRANGE ? 'o'
  1930.                : 'u',
  1931.                this_plot->points[i].x,
  1932.                this_plot->points[i].y,
  1933.                this_plot->points[i].z,
  1934.                this_plot->points[i].ylow,
  1935.                this_plot->points[i].yhigh);
  1936.         }
  1937.         printf("\n");
  1938.     }
  1939.     }
  1940. }
  1941.  
  1942. print_table()
  1943. {
  1944.     register struct curve_points *this_plot;
  1945.     int             i, curve;
  1946.  
  1947.     for (this_plot = first_plot, curve = 0; this_plot != NULL;
  1948.      curve++, this_plot = this_plot->next_cp) {
  1949.     fprintf(outfile, "Curve %d, %d points\n", curve, this_plot->p_count);
  1950.     for (i = 0; i < this_plot->p_count; i++) {
  1951.         fprintf(outfile, "%c x=%g y=%g\n",
  1952.             this_plot->points[i].type == INRANGE ? 'i'
  1953.             : this_plot->points[i].type == OUTRANGE ? 'o'
  1954.             : 'u',
  1955.             this_plot->points[i].x,
  1956.             this_plot->points[i].y);
  1957.     }
  1958.     fprintf(outfile, "\n");
  1959.     }
  1960.     fflush(outfile);
  1961. }
  1962.  
  1963. print_3dtable()
  1964. {
  1965.     register struct surface_points *this_3dplot;
  1966.     int             i, curve;
  1967.     struct gnuplot_contours *contours;    /* Not NULL If have contours. */
  1968.     struct iso_curve *isocrv;
  1969.  
  1970.     for (this_3dplot = first_3dplot, curve = 0; this_3dplot != NULL;
  1971.      curve++, this_3dplot = this_3dplot->next_sp) {
  1972.     isocrv = this_3dplot->iso_crvs;
  1973.     fprintf(outfile, "Curve %d, %d points\n", curve, isocrv->p_count);
  1974.     for (i = 0; i < isocrv->p_count; i++) {
  1975.         fprintf(outfile, "%c x=%g y=%g z=%g\n",
  1976.             isocrv->points[i].type == INRANGE ? 'i'
  1977.             : isocrv->points[i].type == OUTRANGE ? 'o'
  1978.             : 'u',
  1979.             isocrv->points[i].x,
  1980.             isocrv->points[i].y,
  1981.             isocrv->points[i].z);
  1982.     }
  1983.     fprintf(outfile, "\n");
  1984.     }
  1985.     fflush(outfile);
  1986. }
  1987.  
  1988. /*
  1989.  * This parses the plot command after any range specifications. To support
  1990.  * autoscaling on the x axis, we want any data files to define the x range,
  1991.  * then to plot any functions using that range. We thus parse the input
  1992.  * twice, once to pick up the data files, and again to pick up the functions.
  1993.  * Definitions are processed twice, but that won't hurt.
  1994.  */
  1995. eval_plots()
  1996. {
  1997.     register int    i;
  1998.     register struct curve_points *this_plot, **tp_ptr;
  1999.     register int    start_token, end_token;
  2000.     register int    begin_token;
  2001.     double          x_min, x_max, y_min, y_max;
  2002.     register double x, xdiff, temp;
  2003.     static struct value a;
  2004.     TBOOLEAN         ltmp, some_data_files = FALSE;
  2005.     int             plot_num, line_num, point_num, xparam = 0;
  2006.     char           *xtitle;
  2007.     void            parametric_fixup();
  2008.  
  2009.     if (autoscale_ly) {
  2010.     ymin = VERYLARGE;
  2011.     ymax = -VERYLARGE;
  2012.     } else if (is_log_y && (ymin <= 0.0 || ymax <= 0.0))
  2013.     int_error("y range must be above 0 for log scale!",
  2014.           NO_CARET);
  2015.  
  2016.     tp_ptr = &(first_plot);
  2017.     plot_num = 0;
  2018.     line_num = 0;        /* default line type */
  2019.     point_num = 0;        /* default point type */
  2020.  
  2021.     xtitle = NULL;
  2022.  
  2023.     begin_token = c_token;
  2024.  
  2025.     /*** First Pass: Read through data files ***
  2026.      * This pass serves to set the xrange and to parse the command, as well
  2027.      * as filling in every thing except the function data. That is done after
  2028.      * the xrange is defined.
  2029.      */
  2030.     while (TRUE) {
  2031.     if (END_OF_COMMAND)
  2032.         int_error("function to plot expected", c_token);
  2033.  
  2034.     start_token = c_token;
  2035.  
  2036.     if (is_definition(c_token)) {
  2037.         define();
  2038.     } else {
  2039.         plot_num++;
  2040.  
  2041.         if (isstring(c_token)) {    /* data file to plot */
  2042.         if (parametric && xparam)
  2043.             int_error("previous parametric function not fully specified",
  2044.                   c_token);
  2045.  
  2046.         if (!some_data_files && autoscale_lx) {
  2047.             xmin = VERYLARGE;
  2048.             xmax = -VERYLARGE;
  2049.         }
  2050.         some_data_files = TRUE;
  2051.  
  2052.         if (*tp_ptr)
  2053.             this_plot = *tp_ptr;
  2054.         else {        /* no memory malloc()'d there yet */
  2055.             this_plot = cp_alloc(MIN_CRV_POINTS);
  2056.             *tp_ptr = this_plot;
  2057.         }
  2058.         this_plot->plot_type = DATA;
  2059.         this_plot->plot_style = data_style;
  2060.         end_token = c_token;
  2061.         get_data(this_plot);    /* this also parses the using option */
  2062.         } else {        /* function to plot */
  2063.         if (parametric)    /* working on x parametric function */
  2064.             xparam = 1 - xparam;
  2065.         if (*tp_ptr) {
  2066.             this_plot = *tp_ptr;
  2067.             cp_extend(this_plot, samples + 1);
  2068.         } else {    /* no memory malloc()'d there yet */
  2069.             this_plot = cp_alloc(samples + 1);
  2070.             *tp_ptr = this_plot;
  2071.         }
  2072.         this_plot->plot_type = FUNC;
  2073.         this_plot->plot_style = func_style;
  2074.         dummy_func = &plot_func;
  2075.         plot_func.at = temp_at();
  2076.         /* ignore it for now */
  2077.         end_token = c_token - 1;
  2078.         }
  2079.  
  2080.         if (almost_equals(c_token, "t$itle")) {
  2081.         if (parametric) {
  2082.             if (xparam)
  2083.             int_error(
  2084.                      "\"title\" allowed only after parametric function fully specified",
  2085.                      c_token);
  2086.             else if (xtitle != NULL)
  2087.             xtitle[0] = '\0';    /* Remove default title . */
  2088.         }
  2089.         c_token++;
  2090.         if (isstring(c_token)) {
  2091.             m_quote_capture(&(this_plot->title), c_token, c_token);
  2092.         } else {
  2093.             int_error("expecting \"title\" for plot", c_token);
  2094.         }
  2095.         c_token++;
  2096.         } else if (almost_equals(c_token, "not$itle")) {
  2097.             c_token++;
  2098.         } else {
  2099.         m_capture(&(this_plot->title), start_token, end_token);
  2100.         if (xparam)
  2101.             xtitle = this_plot->title;
  2102.         }
  2103.  
  2104.         this_plot->line_type = line_num;
  2105.         this_plot->point_type = point_num;
  2106.  
  2107.         if (almost_equals(c_token, "w$ith")) {
  2108.         if (parametric && xparam)
  2109.             int_error("\"with\" allowed only after parametric function fully specified",
  2110.                   c_token);
  2111.         this_plot->plot_style = get_style();
  2112.         }
  2113.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2114.         struct value    t;
  2115.         this_plot->line_type = (int) real(const_express(&t)) - 1;
  2116.         }
  2117.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2118.         struct value    t;
  2119.         this_plot->point_type = (int) real(const_express(&t)) - 1;
  2120.         }
  2121.         if ((this_plot->plot_style == POINTSTYLE) ||
  2122.         (this_plot->plot_style == LINESPOINTS) ||
  2123.         (this_plot->plot_style == ERRORBARS))
  2124.         if (!xparam)
  2125.             point_num++;
  2126.         if (!xparam)
  2127.         line_num++;
  2128.  
  2129.         if (this_plot->plot_type == DATA)
  2130.         /* now that we know the plot style, adjust the yrange */
  2131.         adjust_yrange(this_plot);
  2132.  
  2133.         tp_ptr = &(this_plot->next_cp);
  2134.     }
  2135.  
  2136.     if (equals(c_token, ","))
  2137.         c_token++;
  2138.     else
  2139.         break;
  2140.     }
  2141.  
  2142.     if (parametric && xparam)
  2143.     int_error("parametric function not fully specified", NO_CARET);
  2144.  
  2145.     if (parametric) {
  2146.     /* Swap t and x ranges for duration of these eval_plot computations. */
  2147.     ltmp = autoscale_lx;
  2148.     autoscale_lx = autoscale_lt;
  2149.     autoscale_lt = ltmp;
  2150.     temp = xmin;
  2151.     xmin = tmin;
  2152.     tmin = temp;
  2153.     temp = xmax;
  2154.     xmax = tmax;
  2155.     tmax = temp;
  2156.     }
  2157.     /*** Second Pass: Evaluate the functions ***/
  2158.     /*
  2159.      * Everything is defined now, except the function data. We expect no
  2160.      * syntax errors, etc, since the above parsed it all. This makes the code
  2161.      * below simpler. If autoscale_ly, the yrange may still change.
  2162.      */
  2163.     if (fabs(xmax - xmin) < zero)
  2164.     if (autoscale_lx) {
  2165.         fprintf(stderr, "Warning: empty %c range [%g:%g], ",
  2166.             parametric ? 't' : 'x', xmin, xmax);
  2167.         if (fabs(xmin) < zero) {
  2168.         /* completely arbitary */
  2169.         xmin = -1.;
  2170.         xmax = 1.;
  2171.         } else {
  2172.         /* expand range by 10% in either direction */
  2173.         xmin = xmin * 0.9;
  2174.         xmax = xmax * 1.1;
  2175.         }
  2176.         fprintf(stderr, "adjusting to [%g:%g]\n", xmin, xmax);
  2177.     } else {
  2178.         int_error("x range is less than `zero`", c_token);
  2179.     }
  2180.  
  2181.     /* give error if xrange badly set from missing datafile error */
  2182.     if (xmin == VERYLARGE || xmax == -VERYLARGE) {
  2183.     int_error("x range is invalid", c_token);
  2184.     }
  2185.     if (is_log_x) {
  2186.     if (xmin <= 0.0 || xmax <= 0.0)
  2187.         int_error("x range must be greater than 0 for log scale!", NO_CARET);
  2188.     x_min = log(xmin)/log_base_log_x;
  2189.     x_max = log(xmax)/log_base_log_x;
  2190.     } else {
  2191.     x_min = xmin;
  2192.     x_max = xmax;
  2193.     }
  2194.  
  2195.     xdiff = (x_max - x_min) / (samples - 1);
  2196.  
  2197.     tp_ptr = &(first_plot);
  2198.     plot_num = 0;
  2199.     this_plot = first_plot;
  2200.     c_token = begin_token;    /* start over */
  2201.  
  2202.     /* Read through functions */
  2203.     while (TRUE) {
  2204.     if (is_definition(c_token)) {
  2205.         define();
  2206.     } else {
  2207.         plot_num++;
  2208.         if (isstring(c_token)) {    /* data file to plot */
  2209.         /* ignore this now */
  2210.         c_token++;
  2211.  
  2212.         /*
  2213.          * jev -- support for passing data from file thru user
  2214.          * function
  2215.          */
  2216.         if (almost_equals(c_token, "thru$")) {
  2217.             struct udft_entry tmp;
  2218.             c_token++;
  2219.             dummy_func = &tmp;
  2220.             (void) temp_at();
  2221.         }
  2222.         if (almost_equals(c_token, "u$sing")) {
  2223.             c_token++;    /* skip "using" */
  2224.             if (!isstring(c_token)) {
  2225.             struct value    a;
  2226.             (void) magnitude(const_express(&a));    /* skip xcol */
  2227.             if (equals(c_token, ":")) {
  2228.                 c_token++;    /* skip ":" */
  2229.                 (void) magnitude(const_express(&a));    /* skip ycol */
  2230.             }
  2231.             if (equals(c_token, ":")) {
  2232.                 c_token++;    /* skip ":" */
  2233.                 (void) magnitude(const_express(&a));    /* skip yemin */
  2234.             }
  2235.             if (equals(c_token, ":")) {
  2236.                 c_token++;    /* skip ":" */
  2237.                 (void) magnitude(const_express(&a));    /* skip yemax */
  2238.             }
  2239.             if (equals(c_token, ":")) {
  2240.                 c_token++;    /* skip ":" */
  2241.                 (void) magnitude(const_express(&a));    /* skip wcol */
  2242.             }
  2243.             }
  2244.             if (isstring(c_token))
  2245.             c_token++;    /* skip format string */
  2246.         }
  2247.         } else {        /* function to plot */
  2248.         if (parametric)    /* working on x parametric function */
  2249.             xparam = 1 - xparam;
  2250.         dummy_func = &plot_func;
  2251.         plot_func.at = temp_at();    /* reparse function */
  2252.  
  2253.         for (i = 0; i < samples; i++) {
  2254.             x = x_min + i * xdiff;
  2255.             /* if (is_log_x) PEM fix logscale x axis */
  2256.             /* x = pow(base_log_x,x); 26-Sep-89 */
  2257.             (void) Gcomplex(&plot_func.dummy_values[0],
  2258.                    is_log_x ? pow(base_log_x, x) : x,
  2259.                    0.0);
  2260.  
  2261.             evaluate_at(plot_func.at, &a);
  2262.  
  2263.             if (undefined || (fabs(imag(&a)) > zero)) {
  2264.             this_plot->points[i].type = UNDEFINED;
  2265.             continue;
  2266.             }
  2267.             temp = real(&a);
  2268.  
  2269.             if (is_log_y && temp < 0.0) {
  2270.             this_plot->points[i].type = UNDEFINED;
  2271.             continue;
  2272.             }
  2273.             this_plot->points[i].x = x;
  2274.             this_plot->points[i].z = -1.0;  /* width of box not specified */
  2275.  
  2276.             if (is_log_y) {
  2277.             if (temp == 0.0) {
  2278.                 this_plot->points[i].type = OUTRANGE;
  2279.                 this_plot->points[i].y = -VERYLARGE;
  2280.                 continue;
  2281.             } else {
  2282.                 this_plot->points[i].y = log(temp)/log_base_log_y;
  2283.             }
  2284.             } else
  2285.             this_plot->points[i].y = temp;
  2286.  
  2287.             if (autoscale_ly || polar
  2288.             || inrange(temp, ymin, ymax)) {
  2289.             this_plot->points[i].type = INRANGE;
  2290.             /* When xparam is 1 we are not really computing y's! */
  2291.             if (!xparam && autoscale_ly) {
  2292.                 if (temp < ymin)
  2293.                 ymin = temp;
  2294.                 if (temp > ymax)
  2295.                 ymax = temp;
  2296.             }
  2297.             } else
  2298.             this_plot->points[i].type = OUTRANGE;
  2299.         }
  2300.         this_plot->p_count = i;    /* samples */
  2301.         }
  2302.  
  2303.         /* title was handled above */
  2304.         if (almost_equals(c_token, "t$itle")) {
  2305.         c_token++;
  2306.         c_token++;
  2307.         } else if (almost_equals(c_token, "not$itle")) {
  2308.             c_token++;
  2309.         }
  2310.         /* style was handled above */
  2311.         if (almost_equals(c_token, "w$ith")) {
  2312.         c_token++;
  2313.         c_token++;
  2314.         }
  2315.         /* line and point types were handled above */
  2316.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2317.         struct value    t;
  2318.         (void) real(const_express(&t));
  2319.         }
  2320.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2321.         struct value    t;
  2322.         (void) real(const_express(&t));
  2323.         }
  2324.         tp_ptr = &(this_plot->next_cp);    /* used below */
  2325.         this_plot = this_plot->next_cp;
  2326.     }
  2327.  
  2328.     if (equals(c_token, ","))
  2329.         c_token++;
  2330.     else
  2331.         break;
  2332.     }
  2333.  
  2334.     /* throw out all curve_points at end of list, that we don't need  */
  2335.     cp_free(*tp_ptr);
  2336.     *tp_ptr = NULL;
  2337.  
  2338.     if (fabs(ymax - ymin) < zero)
  2339.     /* if autoscale, widen range */
  2340.     if (autoscale_ly) {
  2341.         fprintf(stderr, "Warning: empty y range [%g:%g], ", ymin, ymax);
  2342.         if (fabs(ymin) < zero) {
  2343.         ymin = -1.;
  2344.         ymax = 1.;
  2345.         } else {
  2346.         /* expand range by 10% in either direction */
  2347.         ymin = ymin * 0.9;
  2348.         ymax = ymax * 1.1;
  2349.         }
  2350.         fprintf(stderr, "adjusting to [%g:%g]\n", ymin, ymax);
  2351.     } else {
  2352.         int_error("y range is less than `zero`", c_token);
  2353.     }
  2354.  
  2355.     /* Now we finally know the real ymin and ymax */
  2356.     if (is_log_y) {
  2357.     y_min = log(ymin)/log_base_log_y;
  2358.     y_max = log(ymax)/log_base_log_y;
  2359.     } else {
  2360.     y_min = ymin;
  2361.     y_max = ymax;
  2362.     }
  2363.     capture(replot_line, plot_token, c_token);
  2364.  
  2365.     if (parametric) {
  2366.     /* Now put t and x ranges back before we actually plot anything. */
  2367.     ltmp = autoscale_lx;
  2368.     autoscale_lx = autoscale_lt;
  2369.     autoscale_lt = ltmp;
  2370.     temp = xmin;
  2371.     xmin = tmin;
  2372.     tmin = temp;
  2373.     temp = xmax;
  2374.     xmax = tmax;
  2375.     tmax = temp;
  2376.     if (some_data_files && autoscale_lx) {
  2377.         /*
  2378.          * Stop any further autoscaling in this case (may be a mistake,
  2379.          * have to consider what is really wanted some day in the
  2380.          * future--jdc).
  2381.          */
  2382.         autoscale_lx = 0;
  2383.     }
  2384.     /* Now actually fix the plot pairs to be single plots. */
  2385.     parametric_fixup(first_plot, &plot_num, &x_min, &x_max);
  2386.     }
  2387.     if (strcmp(term_tbl[term].name, "table") == 0)
  2388.     print_table();
  2389.     else
  2390.     do_plot(first_plot, plot_num, x_min, x_max, y_min, y_max);
  2391.     cp_free(first_plot);
  2392.     first_plot = NULL;
  2393. }
  2394.  
  2395. static void 
  2396. parse_title(crnt_param, start_token, end_token,
  2397.         xtitle, ytitle, this_plot, do_parse)
  2398. int crnt_param, start_token, end_token;
  2399. char **xtitle, **ytitle;
  2400. struct surface_points *this_plot;
  2401. TBOOLEAN do_parse;
  2402. {
  2403.     static char title[256];
  2404.  
  2405.     if (do_parse) {
  2406.     if (almost_equals(c_token, "t$itle")) {
  2407.         if (parametric) {
  2408.         if (crnt_param)
  2409.             int_error("\"title\" allowed only after parametric function fully specified",
  2410.                   c_token);
  2411.         else {
  2412.             /* Remove default title */
  2413.             if (*xtitle != NULL)
  2414.             (*xtitle)[0] = '\0';
  2415.             if (*ytitle != NULL)
  2416.             (*ytitle)[0] = '\0';
  2417.         }
  2418.         }
  2419.         c_token++;
  2420.         if (isstring(c_token)) {
  2421.         m_quote_capture(&(this_plot->title), c_token, c_token);
  2422.         } else {
  2423.         int_error("expecting \"title\" for plot", c_token);
  2424.         }
  2425.         c_token++;
  2426.     }  else if (almost_equals(c_token, "not$itle")) {
  2427.         c_token++;
  2428.     }  else {
  2429.         m_capture(&(this_plot->title), start_token, end_token);
  2430.         if (crnt_param == 1)
  2431.         *xtitle = this_plot->title;
  2432.         if (crnt_param == 2)
  2433.         *ytitle = this_plot->title;
  2434.     }
  2435.     strncpy(title, this_plot->title, 255);
  2436.     }
  2437.     else {
  2438.     this_plot->title = alloc(strlen(title) + 1);
  2439.     strcpy(this_plot->title, title);
  2440.     }
  2441. }
  2442.  
  2443. /*
  2444.  * This parses the splot command after any range specifications. To support
  2445.  * autoscaling on the x/z axis, we want any data files to define the x/y
  2446.  * range, then to plot any functions using that range. We thus parse the
  2447.  * input twice, once to pick up the data files, and again to pick up the
  2448.  * functions. Definitions are processed twice, but that won't hurt.
  2449.  */
  2450. eval_3dplots()
  2451. {
  2452.     register int    i, j, k;
  2453.     register struct surface_points *this_plot, **tp_3d_ptr;
  2454.     register int    start_token, end_token;
  2455.     register int    begin_token;
  2456.     double          x_min, x_max, y_min, y_max, z_min, z_max;
  2457.     register double x, xdiff, xisodiff, y, ydiff, yisodiff, temp;
  2458.     static struct value a;
  2459.     TBOOLEAN         ltmp, some_data_files = FALSE;
  2460.     int             plot_num, line_num, point_num, crnt_param = 0;    /* 0=x, 1=y, 2=z */
  2461.     char           *xtitle;
  2462.     char           *ytitle;
  2463.     void            parametric_3dfixup();
  2464.  
  2465.     if (autoscale_lz) {
  2466.     zmin = VERYLARGE;
  2467.     zmax = -VERYLARGE;
  2468.     } else if (is_log_z && (zmin <= 0.0 || zmax <= 0.0))
  2469.     int_error("z range must be above 0 for log scale!",
  2470.           NO_CARET);
  2471.  
  2472.     tp_3d_ptr = &(first_3dplot);
  2473.     plot_num = 0;
  2474.     line_num = 0;        /* default line type */
  2475.     point_num = 0;        /* default point type */
  2476.  
  2477.     xtitle = NULL;
  2478.     ytitle = NULL;
  2479.  
  2480.     begin_token = c_token;
  2481.  
  2482.     /*** First Pass: Read through data files ***/
  2483.     /*
  2484.      * This pass serves to set the x/yranges and to parse the command, as
  2485.      * well as filling in every thing except the function data. That is done
  2486.      * after the x/yrange is defined.
  2487.      */
  2488.     while (TRUE) {
  2489.     if (END_OF_COMMAND)
  2490.         int_error("function to plt3d expected", c_token);
  2491.  
  2492.     start_token = c_token;
  2493.  
  2494.     if (is_definition(c_token)) {
  2495.         define();
  2496.     } else {
  2497.         plot_num++;
  2498.  
  2499.         if (isstring(c_token)) {    /* data file to plot */
  2500.         int line_type = line_num,
  2501.             point_type = point_num,
  2502.             plot_style = data_style,
  2503.             first_mesh = TRUE;
  2504.  
  2505.         if (parametric && crnt_param != 0)
  2506.             int_error("previous parametric function not fully specified",
  2507.                   c_token);
  2508.  
  2509.         if (!some_data_files) {
  2510.             if (autoscale_lx) {
  2511.             xmin = VERYLARGE;
  2512.             xmax = -VERYLARGE;
  2513.             }
  2514.             if (autoscale_ly) {
  2515.             ymin = VERYLARGE;
  2516.             ymax = -VERYLARGE;
  2517.             }
  2518.         }
  2519.         some_data_files = TRUE;
  2520.  
  2521.         do {
  2522.             if (*tp_3d_ptr)
  2523.             this_plot = *tp_3d_ptr;
  2524.             else {        /* no memory malloc()'d there yet */
  2525.             /* Allocate enough isosamples and samples */
  2526.             this_plot = sp_alloc(0, 0, 0, 0);
  2527.             *tp_3d_ptr = this_plot;
  2528.             }
  2529.  
  2530.             this_plot->plot_type = DATA3D;
  2531.             end_token = c_token;
  2532.             /* this also parses index/using option */
  2533.             get_3ddata(this_plot);
  2534.  
  2535.             parse_title(crnt_param, start_token, end_token,
  2536.                 &xtitle, &ytitle, this_plot, first_mesh);
  2537.             if (!first_mesh) plot_num++;
  2538.  
  2539.             if (first_mesh) {
  2540.             if (almost_equals(c_token, "w$ith")) {
  2541.                 plot_style = this_plot->plot_style = get_style();
  2542.             }
  2543.             if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2544.                 struct value    t;
  2545.                 line_type = (int) real(const_express(&t)) - 1;
  2546.             }
  2547.             if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2548.                 struct value    t;
  2549.                 point_type = (int) real(const_express(&t)) - 1;
  2550.             }
  2551.             first_mesh = FALSE;
  2552.             }
  2553.  
  2554.             this_plot->line_type = line_type;
  2555.             this_plot->point_type = point_type;
  2556.             this_plot->plot_style = plot_style;
  2557.  
  2558.             tp_3d_ptr = &(this_plot->next_sp);
  2559.         }
  2560.         while (more_data_fp);
  2561.         } else {        /* function to plot */
  2562.         if (parametric)    /* Rotate between x/y/z axes */
  2563.             crnt_param = (crnt_param + 1) % 3;
  2564.         if (*tp_3d_ptr) {
  2565.             this_plot = *tp_3d_ptr;
  2566.             if (!hidden3d)
  2567.             sp_replace(this_plot, samples_1, iso_samples_1,
  2568.                                    samples_2, iso_samples_2);
  2569.             else
  2570.             sp_replace(this_plot, iso_samples_1, 0,
  2571.                                    0, iso_samples_2);
  2572.         } else {    /* no memory malloc()'d there yet */
  2573.             /* Allocate enough isosamples and samples */
  2574.             if (!hidden3d)
  2575.             this_plot = sp_alloc(samples_1, iso_samples_1,
  2576.                                              samples_2, iso_samples_2);
  2577.             else
  2578.             this_plot = sp_alloc(iso_samples_1, 0,
  2579.                                              0, iso_samples_2);
  2580.             *tp_3d_ptr = this_plot;
  2581.         }
  2582.  
  2583.         this_plot->plot_type = FUNC3D;
  2584.         this_plot->has_grid_topology = TRUE;
  2585.         this_plot->plot_style = func_style;
  2586.         dummy_func = &plot_func;
  2587.         plot_func.at = temp_at();
  2588.         /* ignore it for now */
  2589.         end_token = c_token - 1;
  2590.  
  2591.         parse_title(crnt_param, start_token, end_token,
  2592.                 &xtitle, &ytitle, this_plot, TRUE);
  2593.  
  2594.         this_plot->line_type = line_num;
  2595.         this_plot->point_type = point_num;
  2596.  
  2597.         if (almost_equals(c_token, "w$ith")) {
  2598.             this_plot->plot_style = get_style();
  2599.         }
  2600.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2601.             struct value    t;
  2602.             this_plot->line_type = (int) real(const_express(&t)) - 1;
  2603.         }
  2604.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2605.             struct value    t;
  2606.             this_plot->point_type = (int) real(const_express(&t)) - 1;
  2607.         }
  2608.  
  2609.         tp_3d_ptr = &(this_plot->next_sp);
  2610.         }
  2611.  
  2612.         if ((this_plot->plot_style == POINTSTYLE) ||
  2613.         (this_plot->plot_style == LINESPOINTS) ||
  2614.         (this_plot->plot_style == ERRORBARS))
  2615.         if (crnt_param == 0)
  2616.             point_num +=
  2617.             1 + (draw_contour != 0)
  2618.             + (hidden3d != 0);
  2619.         if (crnt_param == 0)
  2620.         line_num += 1 + (draw_contour != 0)
  2621.             + (hidden3d != 0);
  2622.     }
  2623.  
  2624.     if (equals(c_token, ","))
  2625.         c_token++;
  2626.     else
  2627.         break;
  2628.     }
  2629.  
  2630.     if (parametric && crnt_param != 0)
  2631.     int_error("parametric function not fully specified", NO_CARET);
  2632.  
  2633.     if (parametric) {
  2634.     /*
  2635.      * Swap u/v and x/y ranges for duration of these eval_plot
  2636.      * computations.
  2637.      */
  2638.     ltmp = autoscale_lx;
  2639.     autoscale_lx = autoscale_lu;
  2640.     autoscale_lu = ltmp;
  2641.     ltmp = autoscale_ly;
  2642.     autoscale_ly = autoscale_lv;
  2643.     autoscale_lv = ltmp;
  2644.     temp = xmin;
  2645.     xmin = umin;
  2646.     umin = temp;
  2647.     temp = xmax;
  2648.     xmax = umax;
  2649.     umax = temp;
  2650.     temp = ymin;
  2651.     ymin = vmin;
  2652.     vmin = temp;
  2653.     temp = ymax;
  2654.     ymax = vmax;
  2655.     vmax = temp;
  2656.     }
  2657.     /*** Second Pass: Evaluate the functions ***/
  2658.     /*
  2659.      * Everything is defined now, except the function data. We expect no
  2660.      * syntax errors, etc, since the above parsed it all. This makes the code
  2661.      * below simpler. If autoscale_ly, the yrange may still change.
  2662.      */
  2663.     if (xmin == xmax)
  2664.     if (autoscale_lx) {
  2665.         fprintf(stderr, "Warning: empty x range [%g:%g], ",
  2666.             xmin, xmax);
  2667.         if (xmin == 0.0) {
  2668.         /* completely arbitary */
  2669.         xmin = -1.;
  2670.         xmax = 1.;
  2671.         } else {
  2672.         /* expand range by 10% in either direction */
  2673.         xmin = xmin * 0.9;
  2674.         xmax = xmax * 1.1;
  2675.         }
  2676.         fprintf(stderr, "adjusting to [%g:%g]\n", xmin, xmax);
  2677.     } else {
  2678.         int_error("x range is empty", c_token);
  2679.     }
  2680.  
  2681.     if (ymin == ymax)
  2682.     if (autoscale_ly) {
  2683.         fprintf(stderr, "Warning: empty y range [%g:%g], ",
  2684.             ymin, ymax);
  2685.         if (ymin == 0.0) {
  2686.         /* completely arbitary */
  2687.         ymin = -1.;
  2688.         ymax = 1.;
  2689.         } else {
  2690.         /* expand range by 10% in either direction */
  2691.         ymin = ymin * 0.9;
  2692.         ymax = ymax * 1.1;
  2693.         }
  2694.         fprintf(stderr, "adjusting to [%g:%g]\n", ymin, ymax);
  2695.     } else {
  2696.         int_error("y range is empty", c_token);
  2697.     }
  2698.  
  2699.     /* give error if xrange badly set from missing datafile error */
  2700.     if (xmin == VERYLARGE || xmax == -VERYLARGE) {
  2701.     int_error("x range is invalid", c_token);
  2702.     }
  2703.     if (is_log_x) {
  2704.     if (xmin <= 0.0 || xmax <= 0.0)
  2705.         int_error("x range must be greater than 0 for log scale!", NO_CARET);
  2706.     x_min = log(xmin)/log_base_log_x;
  2707.     x_max = log(xmax)/log_base_log_x;
  2708.     } else {
  2709.     x_min = xmin;
  2710.     x_max = xmax;
  2711.     }
  2712.  
  2713.     if (is_log_y) {
  2714.     if (ymin <= 0.0 || ymax <= 0.0)
  2715.         int_error("y range must be greater than 0 for log scale!", NO_CARET);
  2716.     y_min = log(ymin)/log_base_log_y;
  2717.     y_max = log(ymax)/log_base_log_y;
  2718.     } else {
  2719.     y_min = ymin;
  2720.     y_max = ymax;
  2721.     }
  2722.  
  2723.     /*
  2724.      * The next statement is unnecessary because the condition is already
  2725.      * checked in setshow.c. Please remove it.
  2726.      */
  2727.     if (samples_1 < 2 || samples_2 < 2 || iso_samples_1 < 2 || iso_samples_2 < 2)
  2728.     int_error("samples or iso_samples < 2. Must be at least 2.", NO_CARET);
  2729.  
  2730.     if (this_plot->has_grid_topology && hidden3d) {
  2731.     xdiff = (x_max - x_min) / (iso_samples_1 - 1);
  2732.     ydiff = (y_max - y_min) / (iso_samples_2 - 1);
  2733.     } else {
  2734.     xdiff = (x_max - x_min) / (samples_1 - 1);
  2735.     ydiff = (y_max - y_min) / (samples_2 - 1);
  2736.     }
  2737.     xisodiff = (x_max - x_min) / (iso_samples_1 - 1);
  2738.     yisodiff = (y_max - y_min) / (iso_samples_2 - 1);
  2739.  
  2740.     this_plot = first_3dplot;
  2741.     c_token = begin_token;    /* start over */
  2742.  
  2743.     /* Read through functions */
  2744.     while (TRUE) {
  2745.     if (is_definition(c_token)) {
  2746.         define();
  2747.     } else {
  2748.         if (isstring(c_token)) {    /* data file to plot */
  2749.         /* ignore this now */
  2750.         c_token++;
  2751.         if (almost_equals(c_token, "i$ndex")) {
  2752.             struct value a;
  2753.             int index;
  2754.  
  2755.             c_token++;        /* skip "index" */
  2756.             index = (int) magnitude(const_express(&a));
  2757.         }
  2758.         if (almost_equals(c_token, "u$sing")) {
  2759.             c_token++;    /* skip "using" */
  2760.             if (!isstring(c_token)) {
  2761.             struct value    a;
  2762.             (void) magnitude(const_express(&a));    /* skip xcol */
  2763.             if (equals(c_token, ":")) {
  2764.                 c_token++;    /* skip ":" */
  2765.                 (void) magnitude(const_express(&a));    /* skip ycol */
  2766.                 if (equals(c_token, ":")) {
  2767.                 c_token++;    /* skip ":" */
  2768.                 (void) magnitude(const_express(&a));    /* skip zcol */
  2769.                 }
  2770.             }
  2771.             }
  2772.             if (isstring(c_token))
  2773.             c_token++;    /* skip format string */
  2774.         }
  2775.         } else {        /* function to plot */
  2776.         struct iso_curve *this_iso = this_plot->iso_crvs;
  2777.         struct coordinate GPHUGE *points = this_iso->points;
  2778.         int num_sam_to_use, num_iso_to_use;
  2779.  
  2780.         /*
  2781.          * The next statement is useless (crnt_param will not be
  2782.          * used after this point). Please remove it.
  2783.          */
  2784.         if (parametric)
  2785.             crnt_param = (crnt_param + 1) % 3;
  2786.         dummy_func = &plot_func;
  2787.         plot_func.at = temp_at();    /* reparse function */
  2788.  
  2789.         num_iso_to_use = iso_samples_2;
  2790.         if (!(this_plot->has_grid_topology && hidden3d))
  2791.             num_sam_to_use = samples_1;
  2792.         else
  2793.             num_sam_to_use = iso_samples_1;
  2794.         for (j = 0; j < num_iso_to_use; j++) {
  2795.             y = y_min + j * yisodiff;
  2796.             /* if (is_log_y) PEM fix logscale y axis */
  2797.             /* y = pow(log_base_log_y,y); 26-Sep-89 */
  2798.             (void) Gcomplex(&plot_func.dummy_values[1],
  2799.                    is_log_y ? pow(base_log_y, y) : y,
  2800.                    0.0);
  2801.  
  2802.             for (i = 0; i < num_sam_to_use; i++) {
  2803.             x = x_min + i * xdiff;
  2804.             /* if (is_log_x) PEM fix logscale x axis */
  2805.             /* x = pow(base_log_x,x); 26-Sep-89 */
  2806.             (void) Gcomplex(&plot_func.dummy_values[0],
  2807.                        is_log_x ? pow(base_log_x, x) : x,
  2808.                        0.0);
  2809.  
  2810.             points[i].x = x;
  2811.             points[i].y = y;
  2812.  
  2813.             evaluate_at(plot_func.at, &a);
  2814.  
  2815.             if (undefined || (fabs(imag(&a)) > zero)) {
  2816.                 points[i].type = UNDEFINED;
  2817.                 continue;
  2818.             }
  2819.             temp = real(&a);
  2820.  
  2821.             if (is_log_z && temp < 0.0) {
  2822.                 points[i].type = UNDEFINED;
  2823.                 continue;
  2824.             }
  2825.             if (is_log_z) {
  2826.                 if (temp == 0.0) {
  2827.                 points[i].type = OUTRANGE;
  2828.                 points[i].z = -VERYLARGE;
  2829.                 continue;
  2830.                 } else {
  2831.                 points[i].z = log(temp)/log_base_log_z;
  2832.                 }
  2833.             } else
  2834.                 points[i].z = temp;
  2835.  
  2836.             if (autoscale_lz || inrange(temp, zmin, zmax)) {
  2837.                 points[i].type = INRANGE;
  2838.                 if (autoscale_lz) {
  2839.                 if (temp < zmin)
  2840.                     zmin = temp;
  2841.                 if (temp > zmax)
  2842.                     zmax = temp;
  2843.                 }
  2844.             } else
  2845.                 points[i].type = OUTRANGE;
  2846.             }
  2847.             this_iso->p_count = num_sam_to_use;
  2848.             this_iso = this_iso->next;
  2849.             points = this_iso? this_iso->points: NULL;
  2850.         }
  2851.  
  2852.         if (!(this_plot->has_grid_topology && hidden3d)) {
  2853.             num_iso_to_use = iso_samples_1;
  2854.             num_sam_to_use = samples_2;
  2855.             for (i = 0; i < num_iso_to_use; i++) {
  2856.             x = x_min + i * xisodiff;
  2857.             /* if (is_log_x) PEM fix logscale x axis */
  2858.             /* x = pow(base_log_x,x); 26-Sep-89 */
  2859.             (void) Gcomplex(&plot_func.dummy_values[0],
  2860.                        is_log_x ? pow(base_log_x, x) : x,
  2861.                        0.0);
  2862.  
  2863.             for (j = 0; j < num_sam_to_use; j++) {
  2864.                 y = y_min + j * ydiff;
  2865.                 /* if (is_log_y) PEM fix logscale y axis */
  2866.                 /* y = pow(base_log_y,y); 26-Sep-89 */
  2867.                 (void) Gcomplex(&plot_func.dummy_values[1],
  2868.                        is_log_y ? pow(base_log_y, y) : y,
  2869.                        0.0);
  2870.  
  2871.                 points[j].x = x;
  2872.                 points[j].y = y;
  2873.  
  2874.                 evaluate_at(plot_func.at, &a);
  2875.  
  2876.                 if (undefined || (fabs(imag(&a)) > zero)) {
  2877.                 points[j].type = UNDEFINED;
  2878.                 continue;
  2879.                 }
  2880.                 temp = real(&a);
  2881.  
  2882.                 if (is_log_z && temp < 0.0) {
  2883.                 points[j].type = UNDEFINED;
  2884.                 continue;
  2885.                 }
  2886.                 if (is_log_z) {
  2887.                 if (temp == 0.0) {
  2888.                     points[j].type = OUTRANGE;
  2889.                     points[j].z = -VERYLARGE;
  2890.                     continue;
  2891.                 } else {
  2892.                     points[j].z = log(temp)/log_base_log_z;
  2893.                 }
  2894.                 } else
  2895.                 points[j].z = temp;
  2896.  
  2897.                 if (autoscale_lz
  2898.                 || inrange(temp, zmin, zmax)) {
  2899.                 points[j].type = INRANGE;
  2900.                 if (autoscale_lz) {
  2901.                     if (temp < zmin)
  2902.                     zmin = temp;
  2903.                     if (temp > zmax)
  2904.                     zmax = temp;
  2905.                 }
  2906.                 } else
  2907.                 points[j].type = OUTRANGE;
  2908.             }
  2909.             this_iso->p_count = num_sam_to_use;
  2910.             this_iso = this_iso->next;
  2911.             points = this_iso ? this_iso->points : NULL;
  2912.             }
  2913.         }
  2914.         }
  2915.  
  2916.         /* title was handled above */
  2917.         if (almost_equals(c_token, "t$itle")) {
  2918.         c_token++;
  2919.         c_token++;
  2920.         } else if (almost_equals(c_token, "not$itle")) {
  2921.             c_token++;
  2922.         }
  2923.         /* style was handled above */
  2924.         if (almost_equals(c_token, "w$ith")) {
  2925.         c_token++;
  2926.         c_token++;
  2927.         }
  2928.         /* line and point types were handled above */
  2929.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2930.         struct value    t;
  2931.         (void) real(const_express(&t));
  2932.         }
  2933.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2934.         struct value    t;
  2935.         (void) real(const_express(&t));
  2936.         }
  2937.         this_plot = this_plot->next_sp;
  2938.     }
  2939.  
  2940.     if (equals(c_token, ","))
  2941.         c_token++;
  2942.     else
  2943.         break;
  2944.     }
  2945.  
  2946.     if (fabs(zmax - zmin) < zero)
  2947.     /* if autoscale, widen range */
  2948.     if (autoscale_lz) {
  2949.         fprintf(stderr, "Warning: empty z range [%g:%g], ", zmin, zmax);
  2950.         if (fabs(zmin) < zero) {
  2951.         zmin = -1.;
  2952.         zmax = 1.;
  2953.         } else {
  2954.         /* expand range by 10% in either direction */
  2955.         zmin = zmin * 0.9;
  2956.         zmax = zmax * 1.1;
  2957.         }
  2958.         fprintf(stderr, "adjusting to [%g:%g]\n", zmin, zmax);
  2959.     } else {
  2960.         int_error("z range is less than `zero`", c_token);
  2961.     }
  2962.  
  2963.     /* Now we finally know the real zmin and zmax */
  2964.     if (is_log_z) {
  2965.     if (zmin <= 0.0 || zmax <= 0.0)
  2966.         int_error("z range must be greater than 0 for log scale!", NO_CARET);
  2967.     z_min = log(zmin)/log_base_log_z;
  2968.     z_max = log(zmax)/log_base_log_z;
  2969.     } else {
  2970.     z_min = zmin;
  2971.     z_max = zmax;
  2972.     }
  2973.     capture(replot_line, plot_token, c_token);
  2974.  
  2975.     if (parametric) {
  2976.     /* Now put u/v and x/y ranges back before we actually plot anything. */
  2977.     ltmp = autoscale_lx;
  2978.     autoscale_lx = autoscale_lu;
  2979.     autoscale_lu = ltmp;
  2980.     ltmp = autoscale_ly;
  2981.     autoscale_ly = autoscale_lv;
  2982.     autoscale_lv = ltmp;
  2983.     temp = xmin;
  2984.     xmin = umin;
  2985.     umin = temp;
  2986.     temp = xmax;
  2987.     xmax = umax;
  2988.     umax = temp;
  2989.     temp = ymin;
  2990.     ymin = vmin;
  2991.     vmin = temp;
  2992.     temp = ymax;
  2993.     ymax = vmax;
  2994.     vmax = temp;
  2995.  
  2996.     /* Now actually fix the plot triplets to be single plots. */
  2997.     parametric_3dfixup(first_3dplot, &plot_num,
  2998.                &x_min, &x_max, &y_min, &y_max,
  2999.                &z_min, &z_max);
  3000.     if (is_log_x) {
  3001.         if (x_min <= 0.0 || x_max <= 0.0)
  3002.         int_error("x range must be greater than 0 for log scale!", NO_CARET);
  3003.         x_min = log(x_min)/log_base_log_x;
  3004.         x_max = log(x_max)/log_base_log_x;
  3005.     }
  3006.     if (is_log_y) {
  3007.         if (y_min <= 0.0 || y_max <= 0.0)
  3008.         int_error("y range must be greater than 0 for log scale!", NO_CARET);
  3009.         y_min = log(y_min)/log_base_log_y;
  3010.         y_max = log(y_max)/log_base_log_y;
  3011.     }
  3012.     if (is_log_z) {
  3013.         if (z_min <= 0.0 || z_max <= 0.0)
  3014.         int_error("z range must be greater than 0 for log scale!", NO_CARET);
  3015.         z_min = log(z_min)/log_base_log_z;
  3016.         z_max = log(z_max)/log_base_log_z;
  3017.     }
  3018.     }
  3019.  
  3020.     /* Filter out empty meshes. */
  3021.     while (first_3dplot &&
  3022.        first_3dplot->num_iso_read == 0 &&
  3023.        first_3dplot->plot_type == DATA3D) {
  3024.     struct surface_points *plt = first_3dplot->next_sp;
  3025.  
  3026.     first_3dplot->next_sp = NULL;
  3027.     sp_free(first_3dplot);
  3028.     plot_num--;
  3029.     first_3dplot = plt;
  3030.     }
  3031.     if (first_3dplot != NULL) {
  3032.     struct surface_points *plt1, *plt2;
  3033.     
  3034.     for (plt1 = first_3dplot, plt2 = plt1->next_sp; plt2 != NULL; ) {
  3035.         if (plt2->num_iso_read == 0 && plt2->plot_type == DATA3D) {
  3036.         plt2 = plt2->next_sp;
  3037.         plt1->next_sp->next_sp = NULL;
  3038.         sp_free(plt1->next_sp);
  3039.         plot_num--;
  3040.         plt1->next_sp = plt2;
  3041.         }
  3042.         else {
  3043.         plt1 = plt2;
  3044.         plt2 = plt2->next_sp;
  3045.         }
  3046.     }
  3047.     }
  3048.     if (first_3dplot == NULL)
  3049.     int_error("no data found in file", NO_CARET);
  3050.  
  3051.     /* Creates contours if contours are to be plotted as well. */
  3052.     if (draw_contour) {
  3053.     for (this_plot = first_3dplot, i = 0;
  3054.          i < plot_num;
  3055.          this_plot = this_plot->next_sp, i++) {
  3056.         if (this_plot->contours) {
  3057.         struct gnuplot_contours *cntr, *cntrs = this_plot->contours;
  3058.  
  3059.         while (cntrs) {
  3060.             cntr = cntrs;
  3061.             cntrs = cntrs->next;
  3062.             free(cntr->coords);
  3063.             free(cntr);
  3064.         }
  3065.         }
  3066.         /* Make sure this one can be contoured. */
  3067.         if (!this_plot->has_grid_topology) {
  3068.         this_plot->contours = NULL;
  3069.         fprintf(stderr,"Notice: cannot contour non grid data!\n");
  3070.         /* changed from int_error by recommendation of rkc@xn.ll.mit.edu */
  3071.         }
  3072.         else if (this_plot->plot_type == DATA3D)
  3073.         this_plot->contours = contour(
  3074.                          this_plot->num_iso_read,
  3075.                          this_plot->iso_crvs,
  3076.                          contour_levels, contour_pts,
  3077.                          contour_kind, contour_order,
  3078.                          levels_kind, levels_list);
  3079.         else
  3080.         this_plot->contours = contour(iso_samples_2,
  3081.                           this_plot->iso_crvs,
  3082.                           contour_levels, contour_pts,
  3083.                           contour_kind, contour_order,
  3084.                           levels_kind, levels_list);
  3085.     }
  3086.     }
  3087.     if (strcmp(term_tbl[term].name, "table") == 0)
  3088.     print_3dtable();
  3089.     else
  3090.     do_3dplot(first_3dplot, plot_num, x_min, x_max, y_min, y_max, z_min, z_max);
  3091.     sp_free(first_3dplot);
  3092.     first_3dplot = NULL;
  3093. }
  3094.  
  3095. done(status)
  3096.     int             status;
  3097. {
  3098.     if (term && term_init)
  3099.     (*term_tbl[term].reset) ();
  3100. #ifdef vms
  3101.     vms_reset();
  3102. #endif
  3103.     exit(status);
  3104. }
  3105.  
  3106. void 
  3107. parametric_fixup(start_plot, plot_num, x_min, x_max)
  3108.     struct curve_points *start_plot;
  3109.     int            *plot_num;
  3110.     double         *x_min, *x_max;
  3111. /*
  3112.  * The hardest part of this routine is collapsing the FUNC plot types in the
  3113.  * list (which are gauranteed to occur in (x,y) pairs while preserving the
  3114.  * non-FUNC type plots intact.  This means we have to work our way through
  3115.  * various lists.  Examples (hand checked): start_plot:F1->F2->NULL ==>
  3116.  * F2->NULL start_plot:F1->F2->F3->F4->F5->F6->NULL ==> F2->F4->F6->NULL
  3117.  * start_plot:F1->F2->D1->D2->F3->F4->D3->NULL ==> F2->D1->D2->F4->D3->NULL
  3118.  * 
  3119.  * Of course, the more interesting work is to move the y values of the x
  3120.  * function to become the x values of the y function (checking the mins and
  3121.  * maxs as we go along).
  3122.  */
  3123. {
  3124.     struct curve_points *xp, *new_list, *yp = start_plot, *tmp, *free_list,
  3125.                    *free_head = NULL;
  3126.     int             i, tlen, curve;
  3127.     char           *new_title;
  3128.     double          lxmin, lxmax, temp;
  3129.  
  3130.     if (autoscale_lx) {
  3131.     lxmin = VERYLARGE;
  3132.     lxmax = -VERYLARGE;
  3133.     } else {
  3134.     lxmin = xmin;
  3135.     lxmax = xmax;
  3136.     }
  3137.  
  3138.     /*
  3139.      * Ok, go through all the plots and move FUNC types together.  Note: this
  3140.      * originally was written to look for a NULL next pointer, but gnuplot
  3141.      * wants to be sticky in grabbing memory and the right number of items in
  3142.      * the plot list is controlled by the plot_num variable.
  3143.      * 
  3144.      * Since gnuplot wants to do this sticky business, a free_list of
  3145.      * curve_points is kept and then tagged onto the end of the plot list as
  3146.      * this seems more in the spirit of the original memory behavior than
  3147.      * simply freeing the memory.  I'm personally not convinced this sort of
  3148.      * concern is worth it since the time spent computing points seems to
  3149.      * dominate any garbage collecting that might be saved here...
  3150.      */
  3151.     new_list = xp = start_plot;
  3152.     yp = xp->next_cp;
  3153.     curve = 0;
  3154.     for (; curve < *plot_num; xp = xp->next_cp, yp = yp->next_cp, curve++) {
  3155.     if (xp->plot_type != FUNC) {
  3156.         continue;
  3157.     }
  3158.     /* Here's a FUNC parametric function defined as two parts. */
  3159.     --(*plot_num);
  3160.     /*
  3161.      * Go through all the points assigning the y's from xp to be the x's
  3162.      * for yp.  Check max's and min's as you go.
  3163.      */
  3164.     for (i = 0; i < yp->p_count; ++i) {
  3165.         /*
  3166.          * Throw away excess xp points, mark excess yp points as
  3167.          * OUTRANGE.
  3168.          */
  3169.         if (i > xp->p_count) {
  3170.         yp->points[i].type = OUTRANGE;
  3171.         continue;
  3172.         }
  3173.         /*
  3174.          * Just as we had to do when we computed y values--now check that
  3175.          * x's (computed parametrically) are in the permitted ranges as
  3176.          * well.
  3177.          */
  3178.         temp = xp->points[i].y;    /* New x value for yp function. */
  3179.         yp->points[i].x = temp;
  3180.         /* Handle undefined values differently from normal ranges. */
  3181.         if (xp->points[i].type == UNDEFINED)
  3182.         yp->points[i].type = xp->points[i].type;
  3183.         if (autoscale_lx || polar
  3184.         || inrange(temp, lxmin, lxmax)) {
  3185.         if (autoscale_lx && temp < lxmin)
  3186.             lxmin = temp;
  3187.         if (autoscale_lx && temp > lxmax)
  3188.             lxmax = temp;
  3189.         } else
  3190.         yp->points[i].type = OUTRANGE;    /* Due to x value. */
  3191.     }
  3192.     /* Ok, fix up the title to include both the xp and yp plots. */
  3193.     if (xp->title && xp->title[0] != '\0') {
  3194.         tlen = strlen(yp->title) + strlen(xp->title) + 3;
  3195.         new_title = alloc((unsigned long) tlen, "string");
  3196.         strcpy(new_title, xp->title);
  3197.         strcat(new_title, ", ");    /* + 2 */
  3198.         strcat(new_title, yp->title);    /* + 1 = + 3 */
  3199.         free(yp->title);
  3200.         yp->title = new_title;
  3201.     }
  3202.     /* Eliminate the first curve (xparam) and just use the second. */
  3203.     if (xp == start_plot) {
  3204.         /* Simply nip off the first element of the list. */
  3205.         new_list = first_plot = yp;
  3206.         xp = xp->next_cp;
  3207.         if (yp->next_cp != NULL)
  3208.         yp = yp->next_cp;
  3209.         /* Add start_plot to the free_list. */
  3210.         if (free_head == NULL) {
  3211.         free_list = free_head = start_plot;
  3212.         free_head->next_cp = NULL;
  3213.         } else {
  3214.         free_list->next_cp = start_plot;
  3215.         start_plot->next_cp = NULL;
  3216.         free_list = start_plot;
  3217.         }
  3218.     } else {
  3219.         /* Here, remove the xp node and replace it with the yp node. */
  3220.         tmp = xp;
  3221.         /* Pass over any data files that might have been in place. */
  3222.         while (new_list->next_cp && new_list->next_cp != xp)
  3223.         new_list = new_list->next_cp;
  3224.         new_list->next_cp = yp;
  3225.         new_list = new_list->next_cp;
  3226.         xp = xp->next_cp;
  3227.         if (yp->next_cp != NULL)
  3228.         yp = yp->next_cp;
  3229.         /* Add tmp to the free_list. */
  3230.         tmp->next_cp = NULL;
  3231.         if (free_head == NULL) {
  3232.         free_list = free_head = tmp;
  3233.         } else {
  3234.         free_list->next_cp = tmp;
  3235.         free_list = tmp;
  3236.         }
  3237.     }
  3238.     }
  3239.     /* Ok, stick the free list at the end of the curve_points plot list. */
  3240.     while (new_list->next_cp != NULL)
  3241.     new_list = new_list->next_cp;
  3242.     new_list->next_cp = free_head;
  3243.  
  3244.     /* Report the overall graph mins and maxs. */
  3245.     *x_min = lxmin;
  3246.     *x_max = lxmax;
  3247. }
  3248.  
  3249. void 
  3250. parametric_3dfixup(start_plot, plot_num, x_min, x_max, y_min, y_max,
  3251.            z_min, z_max)
  3252.     struct surface_points *start_plot;
  3253.     int            *plot_num;
  3254.     double         *x_min, *x_max, *y_min, *y_max, *z_min, *z_max;
  3255. /*
  3256.  * The hardest part of this routine is collapsing the FUNC plot types in the
  3257.  * list (which are gauranteed to occur in (x,y,z) triplets while preserving
  3258.  * the non-FUNC type plots intact.  This means we have to work our way
  3259.  * through various lists.  Examples (hand checked):
  3260.  * start_plot:F1->F2->F3->NULL ==> F3->NULL
  3261.  * start_plot:F1->F2->F3->F4->F5->F6->NULL ==> F3->F6->NULL
  3262.  * start_plot:F1->F2->F3->D1->D2->F4->F5->F6->D3->NULL ==>
  3263.  * F3->D1->D2->F6->D3->NULL
  3264.  */
  3265. {
  3266.     struct surface_points *xp, *yp, *zp, *new_list, *tmp, *free_list, *free_head = NULL;
  3267.     struct iso_curve *icrvs, *xicrvs, *yicrvs, *zicrvs;
  3268.     int             i, tlen, surface;
  3269.     char           *new_title;
  3270.     double          lxmin, lxmax, lymin, lymax, lzmin, lzmax, temp;
  3271.  
  3272.     if (autoscale_lx) {
  3273.     lxmin = VERYLARGE;
  3274.     lxmax = -VERYLARGE;
  3275.     } else {
  3276.     lxmin = xmin;
  3277.     lxmax = xmax;
  3278.     }
  3279.  
  3280.     if (autoscale_ly) {
  3281.     lymin = VERYLARGE;
  3282.     lymax = -VERYLARGE;
  3283.     } else {
  3284.     lymin = ymin;
  3285.     lymax = ymax;
  3286.     }
  3287.  
  3288.     if (autoscale_lz) {
  3289.     lzmin = VERYLARGE;
  3290.     lzmax = -VERYLARGE;
  3291.     } else {
  3292.     lzmin = zmin;
  3293.     lzmax = zmax;
  3294.     }
  3295.  
  3296.     /*
  3297.      * Ok, go through all the plots and move FUNC3D types together.  Note:
  3298.      * this originally was written to look for a NULL next pointer, but
  3299.      * gnuplot wants to be sticky in grabbing memory and the right number of
  3300.      * items in the plot list is controlled by the plot_num variable.
  3301.      * 
  3302.      * Since gnuplot wants to do this sticky business, a free_list of
  3303.      * surface_points is kept and then tagged onto the end of the plot list
  3304.      * as this seems more in the spirit of the original memory behavior than
  3305.      * simply freeing the memory.  I'm personally not convinced this sort of
  3306.      * concern is worth it since the time spent computing points seems to
  3307.      * dominate any garbage collecting that might be saved here...
  3308.      */
  3309.     new_list = xp = start_plot;
  3310.     for (surface = 0; surface < *plot_num; surface++) {
  3311.     if (xp->plot_type != FUNC3D) {
  3312.         icrvs = xp->iso_crvs;
  3313.  
  3314.         while (icrvs) {
  3315.         struct coordinate GPHUGE *points = icrvs->points;
  3316.  
  3317.         for (i = 0; i < icrvs->p_count; ++i) {
  3318.             if (lxmin > points[i].x)
  3319.             lxmin = points[i].x;
  3320.             if (lxmax < points[i].x)
  3321.             lxmax = points[i].x;
  3322.             if (lymin > points[i].y)
  3323.             lymin = points[i].y;
  3324.             if (lymax < points[i].y)
  3325.             lymax = points[i].y;
  3326.             if (lzmin > points[i].z)
  3327.             lzmin = points[i].z;
  3328.             if (lzmax < points[i].z)
  3329.             lzmax = points[i].z;
  3330.         }
  3331.  
  3332.         icrvs = icrvs->next;
  3333.         }
  3334.         xp = xp->next_sp;
  3335.         continue;
  3336.     }
  3337.     yp = xp->next_sp;
  3338.     zp = yp->next_sp;
  3339.  
  3340.     /* Here's a FUNC3D parametric function defined as three parts. */
  3341.     (*plot_num) -= 2;
  3342.     /*
  3343.      * Go through all the points and assign the x's and y's from xp and
  3344.      * yp to zp.  Check max's and min's as you go.
  3345.      */
  3346.     xicrvs = xp->iso_crvs;
  3347.     yicrvs = yp->iso_crvs;
  3348.     zicrvs = zp->iso_crvs;
  3349.     while (zicrvs) {
  3350.         struct coordinate GPHUGE *xpoints = xicrvs->points, GPHUGE *ypoints = yicrvs->points, GPHUGE *zpoints = zicrvs->points;
  3351.         for (i = 0; i < zicrvs->p_count; ++i) {
  3352.         zpoints[i].x = xpoints[i].z;
  3353.         zpoints[i].y = ypoints[i].z;
  3354.  
  3355.         if (lxmin > zpoints[i].x)
  3356.             lxmin = zpoints[i].x;
  3357.         if (lxmax < zpoints[i].x)
  3358.             lxmax = zpoints[i].x;
  3359.         if (lymin > zpoints[i].y)
  3360.             lymin = zpoints[i].y;
  3361.         if (lymax < zpoints[i].y)
  3362.             lymax = zpoints[i].y;
  3363.         if (lzmin > zpoints[i].z)
  3364.             lzmin = zpoints[i].z;
  3365.         if (lzmax < zpoints[i].z)
  3366.             lzmax = zpoints[i].z;
  3367.         }
  3368.         xicrvs = xicrvs->next;
  3369.         yicrvs = yicrvs->next;
  3370.         zicrvs = zicrvs->next;
  3371.     }
  3372.  
  3373.     /* Ok, fix up the title to include xp and yp plots. */
  3374.     if ((xp->title && xp->title[0] != '\0') ||
  3375.         (yp->title && yp->title[0] != '\0')) {
  3376.         tlen = (xp->title ? strlen(xp->title) : 0) +
  3377.         (yp->title ? strlen(yp->title) : 0) +
  3378.         (zp->title ? strlen(zp->title) : 0) + 5;
  3379.         new_title = alloc((unsigned long) tlen, "string");
  3380.         new_title[0] = 0;
  3381.         if (xp->title) {
  3382.         strcat(new_title, xp->title);
  3383.         strcat(new_title, ", ");    /* + 2 */
  3384.         }
  3385.         if (yp->title) {
  3386.         strcat(new_title, yp->title);
  3387.         strcat(new_title, ", ");    /* + 2 */
  3388.         }
  3389.         if (zp->title) {
  3390.         strcat(new_title, zp->title);
  3391.         }
  3392.         free(zp->title);
  3393.         zp->title = new_title;
  3394.     }
  3395.     /*
  3396.      * Eliminate the first two surfaces (xp and yp) and just use the
  3397.      * third.
  3398.      */
  3399.     if (xp == start_plot) {
  3400.         /* Simply nip off the first two elements of the list. */
  3401.         new_list = first_3dplot = zp;
  3402.         xp = zp->next_sp;
  3403.         /* Add xp and yp to the free_list. */
  3404.         if (free_head == NULL) {
  3405.         free_head = start_plot;
  3406.         } else {
  3407.         free_list->next_sp = start_plot;
  3408.         }
  3409.         free_list = start_plot->next_sp;
  3410.         free_list->next_sp = NULL;
  3411.     } else {
  3412.         /*
  3413.          * Here, remove the xp,yp nodes and replace them with the zp
  3414.          * node.
  3415.          */
  3416.         tmp = xp;
  3417.         /* Pass over any data files that might have been in place. */
  3418.         while (new_list->next_sp && new_list->next_sp != xp)
  3419.         new_list = new_list->next_sp;
  3420.         new_list->next_sp = zp;
  3421.         new_list = zp;
  3422.         xp = zp->next_sp;
  3423.         /* Add tmp to the free_list. */
  3424.         if (free_head == NULL) {
  3425.         free_head = tmp;
  3426.         } else {
  3427.         free_list->next_sp = tmp;
  3428.         }
  3429.         free_list = tmp->next_sp;
  3430.         free_list->next_sp = NULL;
  3431.     }
  3432.     }
  3433.     /* Ok, stick the free list at the end of the surface_points plot list. */
  3434.     while (new_list->next_sp != NULL)
  3435.     new_list = new_list->next_sp;
  3436.     new_list->next_sp = free_head;
  3437.     if (lxmax - lxmin < zero) {
  3438.     if (fabs(lxmax) < zero) {
  3439.         lxmin = -1.0;
  3440.         lxmax = 1.0;
  3441.     } else {
  3442.         lxmin *= 0.9;
  3443.         lxmax *= 1.1;
  3444.     }
  3445.     }
  3446.     if (lymax - lymin < zero) {
  3447.     if (fabs(lymax) < zero) {
  3448.         lymin = -1.0;
  3449.         lymax = 1.0;
  3450.     } else {
  3451.         lymin *= 0.9;
  3452.         lymax *= 1.1;
  3453.     }
  3454.     }
  3455.     if (lzmax - lzmin < zero) {
  3456.     if (fabs(lzmax) < zero) {
  3457.         lzmin = -1.0;
  3458.         lzmax = 1.0;
  3459.     } else {
  3460.         lzmin *= 0.9;
  3461.         lzmax *= 1.1;
  3462.     }
  3463.     }
  3464.     /* Report the overall graph mins and maxs. */
  3465.     if (autoscale_lx) {
  3466.     *x_min = (is_log_x ? pow(base_log_x, lxmin) : lxmin);
  3467.     *x_max = (is_log_x ? pow(base_log_x, lxmax) : lxmax);
  3468.     } else {
  3469.     *x_min = xmin;
  3470.     *x_max = xmax;
  3471.     }
  3472.     if (autoscale_ly) {
  3473.     *y_min = (is_log_y ? pow(base_log_y, lymin) : lymin);
  3474.     *y_max = (is_log_y ? pow(base_log_y, lymax) : lymax);
  3475.     } else {
  3476.     *y_min = ymin;
  3477.     *y_max = ymax;
  3478.     }
  3479.     if (autoscale_lz) {
  3480.     *z_min = (is_log_z ? pow(base_log_z, lzmin) : lzmin);
  3481.     *z_max = (is_log_z ? pow(base_log_z, lzmax) : lzmax);
  3482.     } else {
  3483.     *z_min = zmin;
  3484.     *z_max = zmax;
  3485.     }
  3486. }
  3487.  
  3488. #ifdef AMIGA_SC_6_1
  3489. void 
  3490. sleep(delay)
  3491.     unsigned int    delay;
  3492. {
  3493.     Delay(50 * delay);
  3494. }
  3495. #endif
  3496.  
  3497. #ifdef AMIGA_AC_5
  3498. void 
  3499. sleep(delay)
  3500.     unsigned int    delay;
  3501. {
  3502.     unsigned long   time_is_up;
  3503.     time_is_up = time(NULL) + (unsigned long) delay;
  3504.     while (time(NULL) < time_is_up)
  3505.      /* wait */ ;
  3506. }
  3507. #endif
  3508.  
  3509. #if defined(MSDOS) || defined(_Windows) || defined(DOS386)
  3510. #if (!defined(__TURBOC__) && !defined(__EMX__) && !defined(DJGPP)) || defined(_Windows) /* Turbo C already has sleep() */
  3511. #ifndef __ZTC__            /* ZTC already has usleep() */
  3512. /* kludge to provide sleep() for msc 5.1 */
  3513. void 
  3514. sleep(delay)
  3515.     unsigned int    delay;
  3516. {
  3517.     unsigned long   time_is_up;
  3518.     time_is_up = time(NULL) + (unsigned long) delay;
  3519.     while (time(NULL) < time_is_up)
  3520.      /* wait */ ;
  3521. }
  3522. #endif                /* not ZTC */
  3523. #endif                /* (!TURBOC && !__EMX__ && !DJGPP) or _Windows */
  3524. #endif                /* MSDOS || _Windows*/
  3525.  
  3526.  
  3527. /* Support for input, shell, and help for various systems */
  3528.  
  3529. #ifdef vms
  3530.  
  3531. #include <descrip.h>
  3532. #include <rmsdef.h>
  3533. #include <errno.h>
  3534. #include <smgdef.h>
  3535. #include <smgmsg.h>
  3536.  
  3537. extern          lib$get_input(), lib$put_output();
  3538. extern          smg$read_composed_line();
  3539.  
  3540. int             vms_len;
  3541.  
  3542. unsigned int    status[2] =
  3543. {1, 0};
  3544.  
  3545. static char     help[MAX_LINE_LEN + 1] = "gnuplot";
  3546.  
  3547. $DESCRIPTOR(prompt_desc, PROMPT);
  3548. $DESCRIPTOR(line_desc, input_line);
  3549.  
  3550. $DESCRIPTOR(help_desc, help);
  3551. $DESCRIPTOR(helpfile_desc, "GNUPLOT$HELP");
  3552.  
  3553.  
  3554. read_line(prompt)
  3555.     char           *prompt;
  3556. {
  3557.     int             more, start = 0;
  3558.     char            expand_prompt[40];
  3559.  
  3560.     prompt_desc.dsc$w_length = strlen(prompt);
  3561.     prompt_desc.dsc$a_pointer = prompt;
  3562.     (void) strcpy(expand_prompt, "_");
  3563.     (void) strncat(expand_prompt, prompt, 38);
  3564.     do {
  3565.     line_desc.dsc$w_length = MAX_LINE_LEN - start;
  3566.     line_desc.dsc$a_pointer = &input_line[start];
  3567.     switch (status[1] = smg$read_composed_line(&vms_vkid, 0, &line_desc, &prompt_desc, &vms_len)) {
  3568.     case SMG$_EOF:
  3569.         done(IO_SUCCESS);    /* ^Z isn't really an error */
  3570.         break;
  3571.     case RMS$_TNS:        /* didn't press return in time */
  3572.         vms_len--;        /* skip the last character */
  3573.         break;        /* and parse anyway */
  3574.     case RMS$_BES:        /* Bad Escape Sequence */
  3575.     case RMS$_PES:        /* Partial Escape Sequence */
  3576.         sys$putmsg(status);
  3577.         vms_len = 0;    /* ignore the line */
  3578.         break;
  3579.     case SS$_NORMAL:
  3580.         break;        /* everything's fine */
  3581.     default:
  3582.         done(status[1]);    /* give the error message */
  3583.     }
  3584.     start += vms_len;
  3585.     input_line[start] = '\0';
  3586.     inline_num++;
  3587.     if (input_line[start - 1] == '\\') {
  3588.         /* Allow for a continuation line. */
  3589.         prompt_desc.dsc$w_length = strlen(expand_prompt);
  3590.         prompt_desc.dsc$a_pointer = expand_prompt;
  3591.         more = 1;
  3592.         --start;
  3593.     } else {
  3594.         line_desc.dsc$w_length = strlen(input_line);
  3595.         line_desc.dsc$a_pointer = input_line;
  3596.         more = 0;
  3597.     }
  3598.     } while (more);
  3599.     return 0;
  3600. }
  3601.  
  3602.  
  3603. do_help()
  3604. {
  3605.     help_desc.dsc$w_length = strlen(help);
  3606.     if ((vaxc$errno = lbr$output_help(lib$put_output, 0, &help_desc,
  3607.                &helpfile_desc, 0, lib$get_input)) != SS$_NORMAL)
  3608.     os_error("can't open GNUPLOT$HELP", NO_CARET);
  3609. }
  3610.  
  3611.  
  3612. do_shell()
  3613. {
  3614.     if ((vaxc$errno = lib$spawn()) != SS$_NORMAL) {
  3615.     os_error("spawn error", NO_CARET);
  3616.     }
  3617. }
  3618.  
  3619.  
  3620. do_system()
  3621. {
  3622.     input_line[0] = ' ';    /* an embarrassment, but... */
  3623.  
  3624.     if ((vaxc$errno = lib$spawn(&line_desc)) != SS$_NORMAL)
  3625.     os_error("spawn error", NO_CARET);
  3626.  
  3627.     (void) putc('\n', stderr);
  3628. }
  3629.  
  3630. #else                /* vms */
  3631.  
  3632. #ifdef _Windows
  3633. do_help()
  3634. {
  3635.     if (END_OF_COMMAND)
  3636.         WinHelp(textwin.hWndParent,(LPSTR)winhelpname,HELP_INDEX,(DWORD)NULL);
  3637.     else {
  3638.         char buf[128];
  3639.         int start = c_token++;
  3640.         while (!(END_OF_COMMAND))
  3641.             c_token++;
  3642.         capture(buf, start, c_token-1);
  3643.         WinHelp(textwin.hWndParent,(LPSTR)winhelpname,HELP_PARTIALKEY,(DWORD)buf);
  3644.     }
  3645. }
  3646. #else
  3647.  
  3648. /*
  3649.  * do_help: (not VMS, although it would work) Give help to the user. It
  3650.  * parses the command line into helpbuf and supplies help for that string.
  3651.  * Then, if there are subtopics available for that key, it prompts the user
  3652.  * with this string. If more input is given, do_help is called recursively,
  3653.  * with the argument the index of null character in the string. Thus a more
  3654.  * specific help can be supplied. This can be done repeatedly. If null input
  3655.  * is given, the function returns, effecting a backward climb up the tree.
  3656.  * David Kotz (David.Kotz@Dartmouth.edu) 10/89
  3657.  */
  3658.  
  3659. do_help()
  3660. {
  3661.     static char    *helpbuf = NULL;
  3662.     static char    *prompt = NULL;
  3663.     int             base;    /* index of first char AFTER help string */
  3664.     int             len;    /* length of current help string */
  3665.     TBOOLEAN         more_help;
  3666.     TBOOLEAN         only;    /* TRUE if only printing subtopics */
  3667.     int             subtopics;    /* 0 if no subtopics for this topic */
  3668.     int             start;    /* starting token of help string */
  3669.     char           *help_ptr;    /* name of help file */
  3670. #ifdef ATARI
  3671.     static char    help_fname[256]=""; /* keep helpfilename across calls */
  3672. #endif
  3673.  
  3674.     if ((help_ptr = getenv("GNUHELP")) == (char *) NULL)
  3675. #ifndef ATARI
  3676.     /* if can't find environment variable then just use HELPFILE */
  3677.     help_ptr = HELPFILE;
  3678. #else
  3679.     /* try whether we can find the helpfile via shell_find. If not, just
  3680.        use the default. (tnx Andreas) */
  3681.  
  3682.     if( !strchr( HELPFILE, ':' ) && !strchr( HELPFILE, '/' ) &&
  3683.         !strchr( HELPFILE, '\\' ) ) {
  3684.         if( strlen(help_fname)==0 ) {
  3685.         strcpy( help_fname, HELPFILE );
  3686.         if( shel_find( help_fname )==0 ) {
  3687.             strcpy( help_fname, HELPFILE );
  3688.         }
  3689.         }
  3690.         help_ptr=help_fname;
  3691.     } else {
  3692.         help_ptr=HELPFILE;
  3693.     }
  3694. #endif /* ATARI */
  3695.  
  3696.     /* Since MSDOS DGROUP segment is being overflowed we can not allow such  */
  3697.     /* huge static variables (1k each). Instead we dynamically allocate them */
  3698.     /* on the first call to this function...                     */
  3699.     if (helpbuf == NULL) {
  3700.     helpbuf = alloc((unsigned long)MAX_LINE_LEN, "help buffer");
  3701.     prompt = alloc((unsigned long)MAX_LINE_LEN, "help prompt");
  3702.     helpbuf[0] = prompt[0] = 0;
  3703.     }
  3704.     len = base = strlen(helpbuf);
  3705.  
  3706.     /* find the end of the help command */
  3707.     for (start = c_token; !(END_OF_COMMAND); c_token++);
  3708.     /* copy new help input into helpbuf */
  3709.     if (len > 0)
  3710.     helpbuf[len++] = ' ';    /* add a space */
  3711.     capture(helpbuf + len, start, c_token - 1);
  3712.     squash_spaces(helpbuf + base);    /* only bother with new stuff */
  3713.     lower_case(helpbuf + base);    /* only bother with new stuff */
  3714.     len = strlen(helpbuf);
  3715.  
  3716.     /* now, a lone ? will print subtopics only */
  3717.     if (strcmp(helpbuf + (base ? base + 1 : 0), "?") == 0) {
  3718.     /* subtopics only */
  3719.     subtopics = 1;
  3720.     only = TRUE;
  3721.     helpbuf[base] = '\0';    /* cut off question mark */
  3722.     } else {
  3723.     /* normal help request */
  3724.     subtopics = 0;
  3725.     only = FALSE;
  3726.     }
  3727.  
  3728.     switch (help(helpbuf, help_ptr, &subtopics)) {
  3729.     case H_FOUND:{
  3730.         /* already printed the help info */
  3731.         /* subtopics now is true if there were any subtopics */
  3732.         screen_ok = FALSE;
  3733.  
  3734.         do {
  3735.         if (subtopics && !only) {
  3736.             /* prompt for subtopic with current help string */
  3737.             if (len > 0)
  3738.             (void) sprintf(prompt, "Subtopic of %s: ", helpbuf);
  3739.             else
  3740.             (void) strcpy(prompt, "Help topic: ");
  3741.             read_line(prompt);
  3742.             num_tokens = scanner(input_line);
  3743.             c_token = 0;
  3744.             more_help = !(END_OF_COMMAND);
  3745.             if (more_help)
  3746.             /* base for next level is all of current helpbuf */
  3747.             do_help();
  3748.         } else
  3749.             more_help = FALSE;
  3750.         } while (more_help);
  3751.  
  3752.         break;
  3753.     }
  3754.     case H_NOTFOUND:{
  3755.         printf("Sorry, no help for '%s'\n", helpbuf);
  3756.         break;
  3757.     }
  3758.     case H_ERROR:{
  3759.         perror(help_ptr);
  3760.         break;
  3761.     }
  3762.     default:{            /* defensive programming */
  3763.         int_error("Impossible case in switch", NO_CARET);
  3764.         /* NOTREACHED */
  3765.     }
  3766.     }
  3767.  
  3768.     helpbuf[base] = '\0';    /* cut it off where we started */
  3769. }
  3770. #endif  /* _Windows */
  3771.  
  3772. #ifdef AMIGA_AC_5
  3773. char            strg0[256];
  3774. #endif
  3775.  
  3776. do_system()
  3777. {
  3778. #ifdef AMIGA_AC_5
  3779.     char           *parms[80];
  3780.     void            getparms();
  3781.  
  3782.     getparms(input_line + 1, parms);
  3783.     if (fexecv(parms[0], parms) < 0)
  3784. #else
  3785. #if defined(ATARI)&&defined(__GNUC__)
  3786.     /* use preloaded shell, if available */
  3787.     short           (*shell_p) (char *command);
  3788.     void           *ssp;
  3789.  
  3790.     ssp = (void *) Super(NULL);
  3791.     shell_p = *(short (**) (char *)) 0x4f6;
  3792.     Super(ssp);
  3793.  
  3794.     /* this is a bit strange, but we have to have a single if */
  3795.     if ((shell_p ? (*shell_p) (input_line + 1) : system(input_line + 1)))
  3796. #else
  3797. #ifdef _Windows
  3798.     if (winsystem(input_line + 1))
  3799. #else
  3800.     if (system(input_line + 1))
  3801. #endif
  3802. #endif
  3803. #endif
  3804.     os_error("system() failed", NO_CARET);
  3805. }
  3806.  
  3807. #ifdef AMIGA_AC_5
  3808.  
  3809. /******************************************************************************/
  3810. /* */
  3811. /* Parses the command string (for fexecv use) and  converts the first token  */
  3812. /* to lower case                                                          */
  3813. /* */
  3814. /******************************************************************************/
  3815.  
  3816. void 
  3817. getparms(command, parms)
  3818.     char           *command;
  3819.     char          **parms;
  3820. {
  3821.     register int    i = 0;    /* A bunch of indices          */
  3822.     register int    j = 0;
  3823.     register int    k = 0;
  3824.  
  3825.     while (*(command + j) != '\0') {    /* Loop on string characters   */
  3826.     parms[k++] = strg0 + i;
  3827.     while (*(command + j) == ' ')
  3828.         ++j;
  3829.     while (*(command + j) != ' ' && *(command + j) != '\0') {
  3830.         if (*(command + j) == '"')    /* Get quoted string           */
  3831.         for (*(strg0 + (i++)) = *(command + (j++));
  3832.              *(command + j) != '"';
  3833.              *(strg0 + (i++)) = *(command + (j++)));
  3834.         *(strg0 + (i++)) = *(command + (j++));
  3835.     }
  3836.     *(strg0 + (i++)) = '\0';/* NUL terminate every token   */
  3837.     }
  3838.     parms[k] = '\0';
  3839.  
  3840.     for (k = strlen(strg0) - 1; k >= 0; --k)    /* Convert to lower case       */
  3841.     *(strg0 + k) >= 'A' && *(strg0 + k) <= 'Z' ? *(strg0 + k) |= 32 : *(strg0 + k);
  3842. }
  3843.  
  3844. #endif                /* AMIGA_AC_5 */
  3845.  
  3846. #ifdef READLINE
  3847. char           *
  3848. rlgets(s, n, prompt)
  3849.     char           *s;
  3850.     int             n;
  3851.     char           *prompt;
  3852. {
  3853.     char           *readline();
  3854.     static char    *line = (char *) NULL;
  3855.  
  3856.     /* If we already have a line, first free it */
  3857.     if (line != (char *) NULL)
  3858.     free(line);
  3859.  
  3860.     line = readline((interactive) ? prompt : "");
  3861.  
  3862.     /* If it's not an EOF */
  3863.     if (line) {
  3864.     if (*line)
  3865.         add_history(line);
  3866.     strncpy(s, line, n);
  3867.     return s;
  3868.     }
  3869.     return line;
  3870. }
  3871. #endif                /* READLINE */
  3872.  
  3873. #if defined(MSDOS) || defined(_Windows) || defined(DOS386)
  3874.  
  3875. #ifndef _Windows
  3876. #ifdef __TURBOC__ 
  3877. /* cgets implemented using dos functions */
  3878. /* Maurice Castro 22/5/91 */
  3879. char           *
  3880. doscgets(s)
  3881.     char           *s;
  3882. {
  3883.     long            datseg;
  3884.  
  3885.     /* protect and preserve segments - call dos to do the dirty work */
  3886.     datseg = _DS;
  3887.  
  3888.     _DX = FP_OFF(s);
  3889.     _DS = FP_SEG(s);
  3890.     _AH = 0x0A;
  3891.     geninterrupt(33);
  3892.     _DS = datseg;
  3893.  
  3894.     /* check for a carriage return and then clobber it with a null */
  3895.     if (s[s[1] + 2] == '\r')
  3896.     s[s[1] + 2] = 0;
  3897.  
  3898.     /* return the input string */
  3899.     return (&(s[2]));
  3900. }
  3901. #endif                /* __TURBOC__ */
  3902. #endif                /* !_Windows */
  3903.  
  3904. #ifdef __ZTC__
  3905. void cputs(char *s)
  3906. {
  3907.    register int i = 0;
  3908.    while (s[i] != '\0')  bdos(0x02, s[i++], NULL);
  3909. }
  3910. char *cgets(char *s)
  3911. {
  3912.    bdosx(0x0A, s, NULL);
  3913.  
  3914.    if (s[s[1]+2] == '\r')
  3915.       s[s[1]+2] = 0;
  3916.  
  3917.    /* return the input string */
  3918.    return(&(s[2]));
  3919. }
  3920. #endif   /* __ZTC__ */
  3921.  
  3922.  
  3923. read_line(prompt)
  3924.     char           *prompt;
  3925. {
  3926.     register int    i;
  3927.     int             start = 0, ilen = 0;
  3928.     TBOOLEAN         more;
  3929.     int             last;
  3930.     char           *p, *crnt_prompt = prompt;
  3931.  
  3932.     if (interactive) {        /* if interactive use console IO so CED will
  3933.                  * work */
  3934. #ifndef READLINE
  3935. #if defined(_Windows) || defined(__EMX__) || defined(DJGPP) || defined(__ZTC__)
  3936.     printf("%s", prompt);
  3937. #else
  3938.     cputs(prompt);
  3939. #endif
  3940. #endif                /* READLINE */
  3941.     do {
  3942.         ilen = MAX_LINE_LEN - start - 1;
  3943.         input_line[start] = ilen > 126 ? 126 : ilen;
  3944. #ifdef READLINE
  3945.         input_line[start + 2] = 0;
  3946.         (void) rlgets(&(input_line[start + 2]), ilen, crnt_prompt);
  3947.         if ((p = strchr(&(input_line[start + 2]), '\r'))!=NULL)
  3948.         *p = 0;
  3949.         if ((p = strchr(&(input_line[start + 2]), '\n'))!=NULL)
  3950.         *p = 0;
  3951.         input_line[start + 1] = strlen(&(input_line[start + 2]));
  3952. #else                /* READLINE */
  3953. #if defined(_Windows) || defined(__EMX__) || defined(DJGPP)
  3954.         (void) gets(&(input_line[start+2]));
  3955. #else
  3956. #ifdef __TURBOC__
  3957.         (void) doscgets(&(input_line[start]));
  3958. #else                /* __TURBOC__ */
  3959.         (void) cgets(&(input_line[start]));
  3960. #endif                /* __TURBOC__ */
  3961. #endif                /* _Windows || __EMX__ || DJGPP*/
  3962.         (void) putc('\n', stderr);
  3963. #endif                /* READLINE */
  3964.         if (input_line[start + 2] == 26) {
  3965.         /* end-of-file */
  3966.         (void) putc('\n', stderr);
  3967.         input_line[start] = '\0';
  3968.         inline_num++;
  3969.         if (start > 0)    /* don't quit yet - process what we have */
  3970.             more = FALSE;
  3971.         else {
  3972.             (void) putc('\n', stderr);
  3973.             return(1); /* exit gnuplot */
  3974.             /* NOTREACHED */
  3975.         }
  3976.         } else {
  3977.         /* normal line input */
  3978.         register        i = start;
  3979.         while ((input_line[i] = input_line[i + 2]) != (char) NULL)
  3980.             i++;    /* yuck!  move everything down two characters */
  3981.  
  3982.         inline_num++;
  3983.         last = strlen(input_line) - 1;
  3984.         if (last<0) last=0;  /* stop UAE in Windows */
  3985.         if (last + 1 >= MAX_LINE_LEN)
  3986.             int_error("Input line too long", NO_CARET);
  3987.  
  3988.         if (input_line[last] == '\\') {    /* line continuation */
  3989.             start = last;
  3990.             more = TRUE;
  3991.         } else
  3992.             more = FALSE;
  3993.         }
  3994. #ifndef READLINE
  3995.         if (more)
  3996. #if defined(_Windows) || defined(__EMX__) || defined(DJGPP) || defined(__ZTC__)
  3997.         printf("> ");
  3998. #else
  3999.         cputs("> ");
  4000. #endif
  4001. #else
  4002.         crnt_prompt = "> ";
  4003. #endif                /* READLINE */
  4004.     } while (more);
  4005.     } else {            /* not interactive */
  4006.     if (interactive)
  4007.         fputs(prompt, stderr);
  4008.     do {
  4009.         /* grab some input */
  4010.         if (fgets(&(input_line[start]), MAX_LINE_LEN - start, stdin)
  4011.         == (char *) NULL) {
  4012.         /* end-of-file */
  4013.         if (interactive)
  4014.             (void) putc('\n', stderr);
  4015.         input_line[start] = '\0';
  4016.         inline_num++;
  4017.         if (start > 0)    /* don't quit yet - process what we have */
  4018.             more = FALSE;
  4019.         else
  4020.             return(1);  /* exit gnuplot */
  4021.         } else {
  4022.         /* normal line input */
  4023.         last = strlen(input_line) - 1;
  4024.         if (input_line[last] == '\n') {    /* remove any newline */
  4025.             input_line[last] = '\0';
  4026.             /* Watch out that we don't backup beyond 0 (1-1-1) */
  4027.             if (last > 0)
  4028.             --last;
  4029.             inline_num++;
  4030.         } else if (last + 1 >= MAX_LINE_LEN)
  4031.             int_error("Input line too long", NO_CARET);
  4032.  
  4033.         if (input_line[last] == '\\') {    /* line continuation */
  4034.             start = last;
  4035.             more = TRUE;
  4036.         } else
  4037.             more = FALSE;
  4038.         }
  4039.         if (more && interactive)
  4040.         fputs("> ", stderr);
  4041.     } while (more);
  4042.     }
  4043.     return(0);
  4044. }
  4045.  
  4046.  
  4047. do_shell()
  4048. {
  4049.     register char  *comspec;
  4050.     if ((comspec = getenv("COMSPEC")) == (char *) NULL)
  4051.     comspec = "\\command.com";
  4052. #ifdef _Windows
  4053.     if (WinExec(comspec, SW_SHOWNORMAL) <= 32)
  4054. #else
  4055. #ifdef DJGPP
  4056.     if (system(comspec) == -1)
  4057. #else
  4058.     if (spawnl(P_WAIT, comspec, NULL) == -1)
  4059. #endif
  4060. #endif
  4061.     os_error("unable to spawn shell", NO_CARET);
  4062. }
  4063.  
  4064. #else                /* MSDOS */
  4065. /* plain old Unix */
  4066.  
  4067. read_line(prompt)
  4068.     char           *prompt;
  4069. {
  4070.     int             start = 0;
  4071.     TBOOLEAN         more = FALSE;
  4072.     int             last = 0;
  4073.  
  4074. #ifndef READLINE
  4075.     if (interactive)
  4076.     fputs(prompt, stderr);
  4077. #endif                /* READLINE */
  4078.     do {
  4079.     /* grab some input */
  4080. #ifdef READLINE
  4081.     if (((interactive)
  4082.          ? rlgets(&(input_line[start]), MAX_LINE_LEN - start,
  4083.               ((more) ? "> " : prompt))
  4084.          : fgets(&(input_line[start]), MAX_LINE_LEN - start, stdin))
  4085.         == (char *) NULL) {
  4086. #else
  4087.     if (fgets(&(input_line[start]), MAX_LINE_LEN - start, stdin)
  4088.         == (char *) NULL) {
  4089. #endif                /* READLINE */
  4090.         /* end-of-file */
  4091.         if (interactive)
  4092.         (void) putc('\n', stderr);
  4093.         input_line[start] = '\0';
  4094.         inline_num++;
  4095.         if (start > 0)    /* don't quit yet - process what we have */
  4096.         more = FALSE;
  4097.         else
  4098.         return(1); /* exit gnuplot */
  4099.     } else {
  4100.         /* normal line input */
  4101.         last = strlen(input_line) - 1;
  4102.         if (input_line[last] == '\n') {    /* remove any newline */
  4103.         input_line[last] = '\0';
  4104.         /* Watch out that we don't backup beyond 0 (1-1-1) */
  4105.         if (last > 0)
  4106.             --last;
  4107.         } else if (last + 1 >= MAX_LINE_LEN)
  4108.         int_error("Input line too long", NO_CARET);
  4109.  
  4110.         if (input_line[last] == '\\') {    /* line continuation */
  4111.         start = last;
  4112.         more = TRUE;
  4113.         } else
  4114.         more = FALSE;
  4115.     }
  4116. #ifndef READLINE
  4117.     if (more && interactive)
  4118.         fputs("> ", stderr);
  4119. #endif
  4120.     } while (more);
  4121.     return(0);
  4122. }
  4123.  
  4124. #ifdef VFORK
  4125.  
  4126. do_shell()
  4127. {
  4128.     register char  *shell;
  4129.     register int    p;
  4130.     static int      execstat;
  4131.     if (!(shell = getenv("SHELL")))
  4132.     shell = SHELL;
  4133. #ifdef AMIGA_AC_5
  4134.     execstat = fexecl(shell, shell, NULL);
  4135. #else
  4136.     if ((p = vfork()) == 0) {
  4137.     execstat = execl(shell, shell, NULL);
  4138.     _exit(1);
  4139.     } else if (p == -1)
  4140.     os_error("vfork failed", c_token);
  4141.     else
  4142.     while (wait(NULL) != p)
  4143. #endif
  4144.         ;
  4145.     if (execstat == -1)
  4146.     os_error("shell exec failed", c_token);
  4147.     (void) putc('\n', stderr);
  4148. }
  4149. #else                /* VFORK */
  4150.  
  4151. #ifdef AMIGA_SC_6_1
  4152. do_shell()
  4153. {
  4154.     register char  *shell;
  4155.     if (!(shell = getenv("SHELL")))
  4156.     shell = SHELL;
  4157.  
  4158.     if (system(shell))
  4159.     os_error("system() failed", NO_CARET);
  4160.  
  4161.     (void) putc('\n', stderr);
  4162. }
  4163. #else                /* AMIGA_SC_6_1 */
  4164. #ifdef OS2
  4165. do_shell()
  4166. {
  4167.     register char  *shell;
  4168.     if (!(shell = getenv("COMSPEC")))
  4169.     shell = SHELL;
  4170.  
  4171.     if (system(shell) == -1 )
  4172.     os_error("system() failed", NO_CARET);
  4173.  
  4174.     (void) putc('\n', stderr);
  4175. }
  4176. #else                           /* ! OS2 */
  4177. #define EXEC "exec "
  4178. do_shell()
  4179. {
  4180.     static char     exec[100] = EXEC;
  4181.     register char  *shell;
  4182.     if (!(shell = getenv("SHELL")))
  4183.     shell = SHELL;
  4184.  
  4185.     if (system(strncpy(&exec[sizeof(EXEC) - 1], shell,
  4186.                sizeof(exec) - sizeof(EXEC) - 1)))
  4187.     os_error("system() failed", NO_CARET);
  4188.  
  4189.     (void) putc('\n', stderr);
  4190. }
  4191. #endif                          /* OS2 */
  4192. #endif                /* AMIGA_SC_6_1 */
  4193. #endif                /* VFORK */
  4194. #endif                /* MSDOS */
  4195. #endif                /* vms */
  4196.  
  4197. #ifdef _Windows
  4198. /* there is a system like call on MS Windows but it is a bit difficult to 
  4199.    use, so we will invoke the command interpreter and use it to execute the 
  4200.    commands */
  4201. int winsystem(s)
  4202. char *s;
  4203. {
  4204.     LPSTR comspec;
  4205.     LPSTR execstr;
  4206.     LPSTR p;
  4207.  
  4208.     /* get COMSPEC environment variable */
  4209. #ifdef WIN32
  4210.     char envbuf[81];
  4211.     GetEnvironmentVariable("COMSPEC", envbuf, 80);
  4212.     if (*envbuf == '\0');
  4213.         comspec = "\\command.com";
  4214.     else
  4215.         comspec = envbuf;
  4216. #else
  4217.     p = GetDOSEnvironment();
  4218.     comspec = "\\command.com";
  4219.     while (*p) {
  4220.         if (!strncmp(p,"COMSPEC=",8)) {
  4221.             comspec=p+8;
  4222.             break;
  4223.         }
  4224.         p+=strlen(p)+1;
  4225.     }
  4226. #endif
  4227.     /* if the command is blank we must use command.com */
  4228.     p = s;
  4229.     while ((*p == ' ') || (*p == '\n') || (*p == '\r'))
  4230.         p++;
  4231.     if (*p == '\0')
  4232.     {
  4233.         WinExec(comspec, SW_SHOWNORMAL);
  4234.         }
  4235.     else
  4236.     {
  4237.         /* attempt to run the windows/dos program via windows */
  4238.         if (WinExec(s, SW_SHOWNORMAL) <= 32)
  4239.         {
  4240.             /* attempt to run it as a dos program from command line */
  4241.             execstr = (char *) malloc(strlen(s) + strlen(comspec) + 6);
  4242.             strcpy(execstr, comspec);
  4243.             strcat(execstr, " /c ");
  4244.             strcat(execstr, s);
  4245.             WinExec(execstr, SW_SHOWNORMAL);
  4246.             free(execstr);
  4247.             }
  4248.         }
  4249.  
  4250.     /* regardless of the reality return OK - the consequences of */
  4251.     /* failure include shutting down Windows */
  4252.     return(0);        /* success */
  4253.     }
  4254. #endif
  4255.